Home > Software engineering >  Cast column values to list keeping the order
Cast column values to list keeping the order

Time:09-12

I have the following dataframe:

df1 = pd.DataFrame({'Parent': ['Stay home', "Stay home","Stay home", 'Go swimming', "Go swimming","Go swimming"],
                   'Child': ['Severe weather', "Severe weather", "Severe weather", 'Not Severe weather', "Not Severe weather", "Not Severe weather"],
                   'Child1':   ["Extreme rainy", "Extreme windy",   "Severe snow", "Sunny", "some windy", "No snow"]
                   })

I want to cast the Child1 column to a list groupby the child column

So I am trying the following code:

def cast_to_list(df, col):
    return df.groupby('Child')[col].apply(list).tolist()

list5=cast_to_list(df1, 'Child1')
list5

Outcome:

[['Sunny', 'some windy', 'No snow'],
 ['Extreme rainy', 'Extreme windy', 'Severe snow']]

The expected outcome preserves the order of the values of Child1 column:

So the expected outcome is the following:

  [['Extreme rainy', 'Extreme windy', 'Severe snow'],
     ['Sunny', 'some windy', 'No snow']]

Any ideas?

CodePudding user response:

Use sort=False in groupby:

def cast_to_list(df, col):
    return df.groupby('Child', sort=False)[col].apply(list).tolist()

list5 = cast_to_list(df1, 'Child1')
list5

Output:

[['Extreme rainy', 'Extreme windy', 'Severe snow'],
 ['Sunny', 'some windy', 'No snow']]
  • Related