Python / Pandas - Merge columns with the same index in a single cell -
i have dataframe:
dude group_id 820125 armando 820125 luis oswaldo 64907 bernardo 64907 sandro 64907 veronica
i want this:
dudes group_id 820125 armando | luis oswaldo 64907 bernardo | sandro | veronica
already tried variations of merge, join , concat wasn't successful. idea?
you can group index , join
column:
df.groupby(level=0).agg(' | '.join) # dude #group_id #64907 bernardo | sandro | veronica #820125 armando | luis oswaldo
to join specific column only, use dictionary in agg
function:
df.groupby(level=0).agg({"dude": ' | '.join})
same output above.
Comments
Post a Comment