Skip to content
Advertisement

populating dataframe column from BOOLEAN column with TRUE value

I have this df:

JavaScript

Then I want create a new column, df['category'] that takes in as value, the column’s name whose value is true.

So that df['category'] for each TRUE value column as follows:

JavaScript

NO 2 columns have TRUE value in a row.

Expected output:

JavaScript

Advertisement

Answer

Simple..use idxmax along axis=1 to get the name of column having True value, then map the name to the corresponding category

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement