【发布时间】:2021-12-12 01:23:09
【问题描述】:
我有以下布尔值表:
df1 = pd.DataFrame(data={'w': [True, False, False],
'x': [False, True, False],
'y': [True, True, True],
'z': [True, False, True]},
index=pd.Series([1, 2, 3], name='index'))
| index | w | x | y | z |
|---|---|---|---|---|
| 1 | True | False | True | True |
| 2 | False | True | True | False |
| 3 | False | False | True | True |
我创建了一个与df1宽度相同的新表格:
pd.DataFrame(columns=[f'column{num}' for num in range(1, len(df1.columns) + 1)])
| column1 | column2 | column3 | column4 |
|---|
我想要做的是折叠来自df1 的列,这样对于每一行,我只显示具有True 值的列:
| index | column1 | column2 | column3 | column4 |
|---|---|---|---|---|
| 1 | w | y | z | NA |
| 2 | x | y | NA | NA |
| 3 | y | z | NA | NA |
【问题讨论】:
标签: python pandas series collapse