Hildee
Hildee

Reputation: 89

Pandas dataframe groupby sum strings in reverse order

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?

Upvotes: 1

Views: 153

Answers (2)

jezrael
jezrael

Reputation: 863031

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()

Upvotes: 2

user7864386
user7864386

Reputation:

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

Upvotes: 2

Related Questions