Python: Table where identical ID/Numbers with different values to being them on one line where the different values are appended to the right

Advertisements

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:

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()

Leave a ReplyCancel reply