Home > Enterprise >  I need to groubpy/aggregate with adding a comma
I need to groubpy/aggregate with adding a comma

Time:06-11

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

CodePudding user response:

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
  • Related