0

I want to aggregate in this data frame columns.

data = {'one':['one', 'five', 'one', 'one'],
        'two':['one', 'five', 'one', 'one']}
df = pd.DataFrame(data)
df

Using the following code:

new_df = df.groupby('one').agg(names = ('two', 'sum'))

The output will be:

five    five
one     oneoneone

How to add a comma between the aggregated results?

The wanted result:

five    five
one     one, one, one
  • Does this answer your question? [pandas groupby and join lists](https://stackoverflow.com/questions/23794082/pandas-groupby-and-join-lists) – Chris Jun 10 '22 at 13:51

1 Answers1

0

You just need to add ','.join instead of sum.

 new_df = df.groupby('one').agg(names = ('two', ', '.join))

Output:

             names
one 
five          five
one   one, one, one
R. Baraiya
  • 1,490
  • 1
  • 4
  • 17