I am trying to combine rows of a dataframe in the event that there is a duplicate in one column. The dataframe looks like the following.
Name Code X Y
A 123 10 11
B 456 12 13
C 123 15 16
I want to combine on Code. So if the Code is the same, combine the other data separated by a comma. The resulting df would look like this:
Name Code X Y
A,C 123 10,15 11,16
B 456 12 13
My approach was the following:
df = df.groupby(['Name','Code','Y'])['X'].astype(str).apply(', '.join).reset_index()
df = df.groupby(['Name','Code','X'])['Y'].astype(str).apply(', '.join).reset_index()
I get the following error :
"Cannot access callable attribute 'astype' of 'SeriesGroupBy' objects, try using the 'apply' method"
I have been unable to figure out how to use apply to cast as type str, any tips?