Home > Mobile >  Dataframe add column with list of values from groupby
Dataframe add column with list of values from groupby

Time:02-04

I am trying to generate a list of values, grouped by 'Melder' and add that list as a column to my dataframe. But the apply(list) doesn't work in conjunction with the new_df.insert():

This works, but generates a new Dataframe with only the groupy values

new_df2 = new_df.groupby('Melder')['SAG-Nummer'].apply(list)

This adds a column to my current dataframe, but the values are all NaN

Example:

my_df.insert(1,'Liste',my_df.groupby('Melder')['SAG-ummer'].apply(list))
print(my_df)

      SAG-Nummer Liste   Melder
0  SAG-2001-0389   NaN    Meyer
1  SAG-2001-0388   NaN    Meyer
2  SAG-2001-1833   NaN  Schmidt
3  SAG-2001-1836   NaN     Berg

new_df2 = new_df.groupby('Melder')['SAG-Nummer'].apply(list)
print(my_df2)

Melder
Berg                      [SAG-2001-1836]
Meyer      [SAG-2001-0389, SAG-2001-0388]
Schmidt                   [SAG-2001-1833]

Expected Result:

      SAG-Nummer Liste                            Melder
0  SAG-2001-0389   [SAG-2001-0389, SAG-2001-0388] Meyer
1  SAG-2001-0388   [SAG-2001-0389, SAG-2001-0388] Meyer
2  SAG-2001-1833   [SAG-2001-1833]                Schmidt
3  SAG-2001-1836   [SAG-2001-1836]                Berg

CodePudding user response:

Use the following transformation to expand the result of each group row-wise:

my_df.assign(Liste=my_df.groupby('Melder')['SAG-ummer'].transform(lambda x: [x.values] * len(x)))
  • Related