将十进制拆分为数据框中的多个二进制列

Split decimal to multiple binary column in data frame

数据框有一列'decimal',我需要将十进制转换为特定的二进制列

示例: 3(十进制)--> 0000000000000011(二进制)

df 
| datetime                | mc | vol | decimal |
|-------------------------|----|-----|---------|
| 2021-11-20 12:04:55.107 | PR | 50  | 1       |
| 2021-11-20 12:04:56.187 | PR | 50  | 1       |
| 2021-11-20 12:04:57.200 | PR | 50  | 3       |
| 2021-11-20 12:04:58.310 | PR | 50  | 3       |
| 2021-11-20 12:04:59.467 | PR | 50  | 5       |
| 2021-11-20 12:05:00.500 | PR | 50  | 5       |

第 1 步:使用代码,我得到了以下二进制文件 table。二进制(0~15)

df_test['binary'] = df.decimal.apply(lambda x: format(int(x), '016b'))

| datetime                | mc | vol | binary           |
|-------------------------|----|-----|------------------|
| 2021-11-20 12:04:55.107 | PR | 50  | 0000000000000001 |
| 2021-11-20 12:04:56.187 | PR | 50  | 0000000000000001 |
| 2021-11-20 12:04:57.200 | PR | 50  | 0000000000000011 |
| 2021-11-20 12:04:58.310 | PR | 50  | 0000000000000011 |
| 2021-11-20 12:04:59.467 | PR | 50  | 0000000000000101 |
| 2021-11-20 12:05:00.500 | PR | 50  | 0000000000000101 |

第 2 步:选择值并创建新列

df['B15'] = df['binary'].str[15]
df['B14'] = df['binary'].str[14]
df['B13'] = df['binary'].str[13]
df['B12'] = df['binary'].str[12]
df['B11'] = df['binary'].str[11]

低于要求

| datetime                | mc | vol | B11 | B12 | B13 | B14 | B15  |
|-------------------------|----|-----|-----|-----|-----|-----|------|
| 2021-11-20 12:04:55.107 | PR | 50  | 0   | 0   | 0   | 0   | 1    |
| 2021-11-20 12:04:56.187 | PR | 50  | 0   | 0   | 0   | 0   | 1    |
| 2021-11-20 12:04:57.200 | PR | 50  | 0   | 0   | 0   | 1   | 1    |
| 2021-11-20 12:04:58.310 | PR | 50  | 0   | 0   | 0   | 1   | 1    |
| 2021-11-20 12:04:59.467 | PR | 50  | 0   | 0   | 1   | 0   | 1    |
| 2021-11-20 12:05:00.500 | PR | 50  | 0   | 0   | 1   | 0   | 1    |

还有其他有效的方法吗

如果只需要后5位,可以使用unpackbits:

import pandas as pd
import numpy as np

df = pd.DataFrame({'mc': ['PR', 'PR', 'PR', 'PR', 'PR', 'PR'],
                   'vol': [50, 50, 50, 50, 50, 50],
                   'decimal': [1, 1, 3, 3, 5, 5]})

bits = pd.DataFrame(np.unpackbits(df.decimal.to_numpy(np.uint8)[:, np.newaxis], axis=1)[:,-5:],
                    columns=[f'B{i}' for i in range(11, 16)])
res = pd.concat((df[['mc', 'vol']], bits),axis=1)

结果:

   mc  vol  B11  B12  B13  B14  B15
0  PR   50    0    0    0    0    1
1  PR   50    0    0    0    0    1
2  PR   50    0    0    0    1    1
3  PR   50    0    0    0    1    1
4  PR   50    0    0    1    0    1
5  PR   50    0    0    1    0    1