'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?
Solution 1:[1]
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()
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jezrael |
| Solution 2 |
