数据帧有一个“十进制”列,我需要将十进制转换成特定的二进制列。
示例: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:使用代码,我得到了下面的二进制表。二进制(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 |有没有其他有效的方法。
发布于 2021-12-23 14:51:47
如果只需要最后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 1https://stackoverflow.com/questions/70460140
复制相似问题