0

I have a dataframe:

df = pd.DataFrame([
    {'a': 1, 'b': 'abc'},
    {'a': 1, 'b': 'def'},
    {'a': 2, 'b': 'abc'},
    {'a': 2, 'b': 'def'},
    {'a': 2, 'b': 'ghi'},
])

And I would like to get the resulting dataframe as so:

df = pd.DataFrame([
    {'a': 1, 'b': ['abc', 'def']},
    {'a': 2, 'b': ['abc', 'def', 'ghi']}
])

I suspect something like :

df_new = df.groupby(df['a'])

but I need to somehow "aggregrate" strings into one cell (as a list). How do I achieve this?

mozway
  • 194,879
  • 13
  • 39
  • 75
pymat
  • 1,090
  • 1
  • 23
  • 45

0 Answers0