1

I have the below dataframe from which I need to groupby using id column and the corresponding values should be in list at the same cell. Anyone please help me on this?

I have this processed Dataframe:

enter image description here

Actual Dataframe:

enter image description here

In the actual dataframe, the list values should be added in the new column called e_values to the respective id.

Beginner
  • 143
  • 1
  • 12
  • 1
    Does this answer your question? [Pandas - combine column values into a list in a new column](https://stackoverflow.com/questions/43898035/pandas-combine-column-values-into-a-list-in-a-new-column) – mozway Sep 20 '21 at 13:43
  • Where are you using `groupby`? – not_speshal Sep 20 '21 at 13:48

2 Answers2

-1
df['e_values'] = df.filter(like='col_').apply(list, axis=1)
mozway
  • 194,879
  • 13
  • 39
  • 75
-1

If going from Actual to processed; You can split, expand=True and then replace the corner brackets. This should give you a dataframe which you can rename columns

df['values'].str.split(',',expand=True).replace(regex={'\[': '', '\]': ''})

If going from processed to Actual use:

df.set_index('id').agg(list,1).reset_index()
wwnde
  • 26,119
  • 6
  • 18
  • 32
  • @not_speshal If thats the case he can go the other way thus; `df.set_index('id').agg(list,1).reset_index()` – wwnde Sep 20 '21 at 14:19