0

I am trying to bring values from rows of a data frame into a new column as a string, separated by comma.

df = pd.DataFrame({'category': [1,1,1,2,2], 'value': [1,3,4,2,3]})

   category  value
0  1         1
1  1         3
2  1         4
3  2         2
4  2         3

and my goal would be a df as follows:

   category  agg_values
0  1         1,3,4
1  2         2,3

I tried the following, but I get an 'AttributeError: 'SeriesGroupBy' object has no attribute 'str'' error.

df['agg_values'] = df.groupby('category').value.str.cat(sep=',')

Do you have any idea what my mistake is here? Thank you!

Carlos
  • 17
  • 4

0 Answers0