I have a dataframe like this with Boolean values
| black | yellow | orange |
|---|---|---|
| TRUE | TRUE | TRUE |
| FALSE | TRUE | FALSE |
| TRUE | TRUE | FALSE |
| FALSE | FALSE | TRUE |
I want a separate column that summarizes the Boolean values based on column name which the column would be
| summary |
|---|
| black, yellow, orange |
| yellow |
| black, yellow |
| orange |
Any idea how to do this please? Thanks!
You can use each row as a selection mask to filter the column names:
(
df.astype("bool")
.apply(lambda row: ", ".join(df.columns[row]), axis=1)
.to_frame("summary")
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With