Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python pandas – convert multiple columns to row

I have a table like this

id non-binary female male others
1 True False False False
2 False True False False
3 False False True False
4 True True True False

I want to combine columns into row like this –
If multiple columns have value True, only use the first column that has value True. Eg, for id=4, since the first True is in column "non-binary", then in the new table, set the gender value to non-binary.
How can it be converted to the table below by using Pandas?

id Gender
1 non-binary
2 female
3 male
4 non-binary

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since boolean values are numeric (True == 1, False == 0) you can use idxmax to get the index of the maximum value. This interestingly will correspond to the name of the column where the first True appears if you apply this along the column axis.

# set your index to the id column if it is not already
df = df.set_index('id') 

out = df.idxmax(axis='columns').rename('Gender')

print(out)
id
1    non-binary
2        female
3          male
4    non-binary
Name: Gender, dtype: object


# Can use .reset_index() or .to_frame() to convert back to a DataFrame 
#   (depending on what you want to do with your Index)
print(out.reset_index())
   id      Gender
0   1  non-binary
1   2      female
2   3        male
3   4  non-binary
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading