gpt4 book ai didi

python - 解码列并使用新数据创建新数据框

转载 作者:行者123 更新时间:2023-12-05 03:27:19 25 4
gpt4 key购买 nike

我有一个具有以下结构的数据框:

Timestamp         Payload                                                                                                     
2022-03-16 18:00 3c313034393536353534303b31332e32333534313b303b302e303637313b302e303138313b332e33353b36322e31353338323e
2022-03-16 18:10 3c313034393536363134303b31332e32313134383b303b302e303637323b302e3031383b342e3632353b37332e34353133313e
2022-03-16 18:20 3c313034393536363734303b31332e31393630313b303b302e303637323b302e3031383b342e303132353b36322e34343238353e
2022-03-16 18:30 3c313034393537353734303b31332e31303137343b303b302e3036373b302e303137393b342e383132353b37352e343931393e
2022-03-16 18:40 3c313034393538323934303b31332e30353934353b303b302e303636373b302e303137393b332e313337353b37352e37313830393e

我想要的是在这两个字段出现的地方创建一个新的数据帧,以及有效负载列的解码。我遇到的困难是对那组字符的解码会生成一组数据。例如,如果我们解码以下行,

3c313034393536353534303b31332e32333534313b303b302e303637313b302e303138313b332e33353b36322e31353338323e

我们得到,

<1049565540;13.23541;0;0.0671;0.0181;3.35;62.15382>

其中字符< , >;是分隔符。所以我的目标是获得类似于以下数据框的内容,

2022-03-16 18:00  1049565540  13.23541  0  0.0671  0.0181  3.3500  62.15382  3c313034393536353534303b31332e32333534313b303b302e303637313b302e303138313b332e33353b36322e31353338323e
2022-03-16 18:10 1049566140 13.21148 0 0.0672 0.0180 4.6250 73.45131 3c313034393536363134303b31332e32313134383b303b302e303637323b302e3031383b342e3632353b37332e34353133313e
2022-03-16 18:20 1049566740 13.19601 0 0.0672 0.0180 4.0125 62.44285 3c313034393536363734303b31332e31393630313b303b302e303637323b302e3031383b342e303132353b36322e34343238353e
2022-03-16 18:30 1049575740 13.10174 0 0.0670 0.0179 4.8125 75.49190 3c313034393537353734303b31332e31303137343b303b302e3036373b302e303137393b342e383132353b37352e343931393e
2022-03-16 18:40 1049582940 13.05945 0 0.0667 0.0179 3.1375 75.71809 3c313034393538323934303b31332e30353934353b303b302e303636373b302e303137393b332e313337353b37352e37313830393e

实际上我正在执行以下代码,没关系,但我确定我没有利用 Pandas 的潜力图书馆。

def myFunc(pay):
decode = bytearray.fromhex(pay[2:-2]).decode()
return decode.split(";")

output2['datos'] = output2['payload'].apply(myFunc)
payloads = output2['datos'].values
df = pd.DataFrame(payloads.tolist(), columns=None)

能否以更高效的方式完成?

非常感谢!

最佳答案

如果想要通过特殊功能工作是可能的,请使用 .apply 作为您的解决方案或列表理解:

L = [bytearray.fromhex(pay[2:-2]).decode().split(";") for pay in output2.pop('Payload')]
df = pd.DataFrame(L, index=output2.index)

上次使用加入:

df = output2.join(df)
print (df)
Timestamp 0 1 2 3 4 5 6
0 2022-03-16 18:10 1049566140 13.21148 0 0.0672 0.018 4.625 73.45131
1 2022-03-16 18:20 1049566740 13.19601 0 0.0672 0.018 4.0125 62.44285
2 2022-03-16 18:30 1049575740 13.10174 0 0.067 0.0179 4.8125 75.4919
3 2022-03-16 18:40 1049582940 13.05945 0 0.0667 0.0179 3.1375 75.71809

如果还需要原创专栏:

L = [bytearray.fromhex(pay[2:-2]).decode().split(";") for pay in output2['Payload']]
df = pd.DataFrame(L, index=output2.index)
df = output2.join(df)
print (df)
Timestamp Payload \
0 2022-03-16 18:10 3c313034393536363134303b31332e32313134383b303b...
1 2022-03-16 18:20 3c313034393536363734303b31332e31393630313b303b...
2 2022-03-16 18:30 3c313034393537353734303b31332e31303137343b303b...
3 2022-03-16 18:40 3c313034393538323934303b31332e30353934353b303b...

0 1 2 3 4 5 6
0 1049566140 13.21148 0 0.0672 0.018 4.625 73.45131
1 1049566740 13.19601 0 0.0672 0.018 4.0125 62.44285
2 1049575740 13.10174 0 0.067 0.0179 4.8125 75.4919
3 1049582940 13.05945 0 0.0667 0.0179 3.1375 75.71809

关于python - 解码列并使用新数据创建新数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71525392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com