将字符串转换二进制到各个位蟒蛇数据帧

问题描述:

我有一个熊猫数据框,看起来像这样,重复约10K行:将字符串转换二进制到各个位蟒蛇数据帧

 Lbl #    Value       Time 
16 160 0-00-000-0000-0000-0000-0000-00 000:00:00:00.206948 
17 270 0-00-000-0000-0001-1010-0110-00 000:00:00:00.212948 
18 271 1-00-000-0000-0000-0110-1110-00 000:00:00:00.215828 
19 272 0-00-001-1000-0111-1111-1000-00 000:00:00:00.218708 
20 273 1-00-000-0000-0000-0111-1110-00 000:00:00:00.221588 
21 274 0-00-000-0000-0000-1001-0110-00 000:00:00:00.224468 
22 275 0-00-001-1111-0000-0000-0000-00 000:00:00:00.227348 
23 276 1-00-000-0000-0000-0000-0000-00 000:00:00:00.233428 
24 277 0-00-000-0000-0000-0000-0000-00 000:00:00:00.236308 
29 334 0-11-000-0000-0000-0000-0000-00 000:00:00:00.253900 
63 160 0-00-000-0000-0000-0000-0000-00 000:00:00:00.458692 

我该如何去到每一个“值”标签,并把它分解成24个相应的位。结束游戏将能够在数据文件的过程中绘制标签160,位19以及其他一些分析。

谢谢。

编辑:MaxU的答案工作。只是为了将来的游客,我结束了最后的代码是:

df_bits = df_binary.Value.str.replace('-','').str.extractall('(\d)').unstack().astype(np.int8).add_prefix('b') 
df_binary = pd.concat([df_binary, df_bits], axis = 1) 
+0

是否使用基本字节功能此帮助:0-00-000-0000 bytez =字节(' -0000-0000-0000-00','ascii')[b for bytez] – skrubber

IIUC:

In [43]: df 
Out[43]: 
    Lbl #       Value     Time 
0 16 160 0-00-000-0000-0000-0000-0000-00 000:00:00:00.206948 
1 17 270 0-00-000-0000-0001-1010-0110-00 000:00:00:00.212948 
2 18 271 1-00-000-0000-0000-0110-1110-00 000:00:00:00.215828 
3 19 272 0-00-001-1000-0111-1111-1000-00 000:00:00:00.218708 
4 20 273 1-00-000-0000-0000-0111-1110-00 000:00:00:00.221588 
5 21 274 0-00-000-0000-0000-1001-0110-00 000:00:00:00.224468 
6 22 275 0-00-001-1111-0000-0000-0000-00 000:00:00:00.227348 
7 23 276 1-00-000-0000-0000-0000-0000-00 000:00:00:00.233428 
8 24 277 0-00-000-0000-0000-0000-0000-00 000:00:00:00.236308 
9 29 334 0-11-000-0000-0000-0000-0000-00 000:00:00:00.253900 
10 63 160 0-00-000-0000-0000-0000-0000-00 000:00:00:00.458692 

In [44]: df.Value.str.replace('-','').str.extractall('(\d)').unstack().astype(np.int8).add_prefix('b') 
Out[44]: 
     b0       ... 
match b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ... b14 b15 b16 b17 b18 b19 b20 b21 b22 b23 
0  0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 
1  0 0 0 0 0 0 0 0 0 0 ... 1 0 1 0 0 1 1 0 0 0 
2  1 0 0 0 0 0 0 0 0 0 ... 0 1 1 0 1 1 1 0 0 0 
3  0 0 0 0 0 1 1 0 0 0 ... 1 1 1 1 1 0 0 0 0 0 
4  1 0 0 0 0 0 0 0 0 0 ... 0 1 1 1 1 1 1 0 0 0 
5  0 0 0 0 0 0 0 0 0 0 ... 1 0 0 1 0 1 1 0 0 0 
6  0 0 0 0 0 1 1 1 1 1 ... 0 0 0 0 0 0 0 0 0 0 
7  1 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 
8  0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 
9  0 1 1 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 
10  0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0 

[11 rows x 24 columns] 
+1

完美。看起来正则表达式可能只是“'(\ d)”。 –

+0

@PauloScardine,的确,谢谢! – MaxU