Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas create new column based on boolean value

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!

like image 326
siusiusiu Avatar asked Dec 18 '25 18:12

siusiusiu


1 Answers

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")
)
like image 117
Code Different Avatar answered Dec 21 '25 07:12

Code Different



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!