Skip to content Skip to sidebar Skip to footer

Groupby Separate Sum With Commas

I have a python data frame and when i use this command df.groupby(['A','B'])['C'].sum() i get the objects of column C as one because they are strings. What i want to get is all the

Solution 1:

Use join to explicitly add a separator, which you can't do with sum:

df.groupby(['A','B'])['C'].apply(", ".join)

In [11]: ", ".join(["a", "b", "c"])
Out[11]: 'a, b, c'

Post a Comment for "Groupby Separate Sum With Commas"