簡體   English   中英

將十進制拆分為數據框中的多個二進制列

[英]Split decimal to multiple binary column in data frame

數據框有一列“十進制”,我需要將十進制轉換為特定的二進制列

示例:3(十進制)--> 00000000000 00011 (二進制)

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    |

有沒有其他有效的方法。

如果您只需要最后 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM