我有一个很大的DataFrame(1999048行和1col),带有十六进制数据。我想将每一行以二进制形式,切成小块,然后以十进制格式进行处理。
我尝试过:
for i in range (len(df.index)):
hexa_line=hex2bin(str(f1.iloc[i]))[::-1]
channel = int(hexa_line[0:3][::-1], 2)
edge = int(hexa_line[3][::-1], 2)
time = int(hexa_line[4:32][::-1], 2)
sweep = int(hexa_line[32:48][::-1], 2)
tag = int(hexa_line[48:63][::-1], 2)
datalost = int(hexa_line[63][::-1], 2)
line=np.array([[channel, edge, time, sweep, tag, datalost]])
tab=np.concatenate((tab, line), axis=0)
但这真的很长。...有更快的方法吗?
答案 0 :(得分:0)
我想不到的很多事情就是改变这些行:
line=np.array([[channel, edge, time, sweep, tag, datalost]])
tab=np.concatenate((tab, line), axis=0)
当然在熊猫中,我认为在numpy压缩中也是一件昂贵的事情,它取决于两个数组的总大小(而不是list.append)
我认为这样做是每次调用它都会重写整个数组tab
。也许您可以尝试将每行添加到列表中,然后将整个列表隐藏在一起。
例如更像这样的东西:
tab = []
for i in range (len(df.index)):
hexa_line=hex2bin(str(f1.iloc[i]))[::-1]
channel = int(hexa_line[0:3][::-1], 2)
edge = int(hexa_line[3][::-1], 2)
time = int(hexa_line[4:32][::-1], 2)
sweep = int(hexa_line[32:48][::-1], 2)
tag = int(hexa_line[48:63][::-1], 2)
datalost = int(hexa_line[63][::-1], 2)
line=np.array([[channel, edge, time, sweep, tag, datalost]])
tab.append(line)
final_tab = np.concatenate(tab, axis=0)
# or whatever the syntax is :p