-1

I have a dataframe (pandas python) in this format:

ID Valor
1 15
1 10
1 25
2 13
3 10
3 11

What I want to do is join the data by id into a list so that it looks like this:

ID Valor
1 [15,10,25]
2 13
3 [10,11]

Someone can help me, please?

  • Does this answer your question? [How to implode(reverse of pandas explode) based on a column](https://stackoverflow.com/questions/64235312/how-to-implodereverse-of-pandas-explode-based-on-a-column) – Nick ODell Jan 04 '23 at 22:17

1 Answers1

1

Use:

df2 = df.groupby('ID', as_index = False)['Valor'].agg(list)
user19077881
  • 3,643
  • 2
  • 3
  • 14