2

I have a pandas dataframe that looks like this, and repeats for about 10k rows:

     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  

How do I go into each 'Value' label and break it up into the 24 corresponding bits. End game is to be able to graph Label 160, Bit 19 over the course of the datafile, along with some other analysis.

Thanks.

Edit: The answer from MaxU worked. Just for future visitors, the final code I ended up with is:

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)
1
  • Does this help using the base bytes function: bytez = bytes('0-00-000-0000-0000-0000-0000-00', 'ascii') [b for b in bytez] Commented Oct 16, 2017 at 17:53

1 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Looks like the regular expression could be just '(\d)'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.