I have the following to tables of boolean values:
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 |
I have created a new table with the same width as df1:
pd.DataFrame(columns=[f'column{num}' for num in range(1, len(df1.columns) + 1)])
| column1 | column2 | column3 | column4 |
|---|
What I want to do is collapse the columns from df1 so that for each row, I am only showing the columns that have True values:
| index | column1 | column2 | column3 | column4 |
|---|---|---|---|---|
| 1 | w | y | z | NA |
| 2 | x | y | NA | NA |
| 3 | y | z | NA | NA |