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

How to format pandas dataframe in order to just keep one row per id?

I have such a pandas dataframe:

id feature1 feature feature3
A 1 0 0
A 0 1 0
B 0 1 0
B 1 0 0
B 0 0 1
C 0 0 1

So, this means this is a one hot encoded dataframe. I would like to get it now in a another format in order to just have one row per id:

id feature1 feature feature3
A 1 1 0
B 1 1 1
C 0 0 1

How can I do this?

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 :

Use groupby.max:

out = df.groupby('id', as_index=False).max()

output:

  id  feature1  feature  feature3
0  A         1        1         0
1  B         1        1         1
2  C         0        0         1
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