Home > Software design >  Pandas dataframe groupby sum strings in reverse order
Pandas dataframe groupby sum strings in reverse order

Time:03-10

I have a DataFrame like this:

colA    colB 
1       aaa
1       rrr
1       www
2       bbb
2       ccc
2       sss
...

I would like to convert the DataFrame as follows

colA    Sum
1       wwwrrraaa
2       ssscccbbb
...

I tried

df.groupby(['colA'])['colB'].sum().reset_index()

but the sum of strings are reversed. Is there an elegant way to do this?

CodePudding user response:

Don't use sum to concatenate strings. It looks fancy but it's quadratic and should be considered bad practice. In python is use function join

df = df[::-1].groupby('colA')['colB'].agg(''.join).reset_index()

CodePudding user response:

Reverse the DataFrame; then groupby sum:

out = df[::-1].groupby('colA', as_index=False)['colB'].sum()

Output:

   colA       colB
0     1  wwwrrraaa
1     2  ssscccbbb
  • Related