Suppose the following df:
df = pd.DataFrame(
{
'col 1': ['A', 'A', 'B', 'B', 'C'],
'col 2': ['c2', 'd2', 'e2', 'f2', 'g2'],
'col 3': ['c3', 'd3', 'e3', 'f3', 'g3'],
'col 4': ['c4', 'd4', 'e4', 'f4', 'g4'],
}
)
Desired output is:
col 1 col 2 col 3
A c2;d2 c3;d3
B e2;f2 e3;f3
C g2 g3
I've succeeded in doing this in the following way:
df = df.groupby('col 1').transform( lambda x: ';'.join(x)).drop_duplicates()
The problem is that this method does not keep column 1 which i really need. and i couldn't get apply working. I've tried like this but it doesn't seem to work after .groupby.
apply(lambda x: '*'.join(x.dropna().values.tolist()), axis=1)