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: Table where identical ID/Numbers with different values to being them on one line where the different values are appended to the right

I have a Pandas Table with some IDs that are identical on several lines but the assigned value is different. How is it possible to get a result where the ID is only shown once on one line and append the various values in multiple columns?

Starting point:

ID Column 1
1 blue
1 red
2 gray
3 yellow
4 orange
1 pink
2 white

Desired solution:

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

ID Column 1 Column 2 Column 3
1 blue red pink
2 gray white
3 yellow
4 orange

>Solution :

Groupby the ID and then compute the unique values

df.groupby("ID")["Column 1"].apply(lambda x: pd.Series(x.unique())).unstack()
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