0

I have dataframe like this

current dataframe

Is there any way to convert to this

desired dataframe

I tried to traverse data frame than traverse grades array and add add to new data frame but it doesn't seem most efficient or easy way is there any built in method or better way to approach this problem

PS: I searched for similar questions but I couldn't find it if there is question like this I am very sorry ı will delete immediately

Mort
  • 3,379
  • 1
  • 25
  • 40
SnoopyDog
  • 33
  • 9

1 Answers1

1

What you want is pandas.DataFrame.explode().

import ast

# Make sure column A is list first.
df['A'] = df['A'].apply(ast.literal_eval)
# or
df['A'] = df['A'].apply(pd.eval)

df = df.explode('Grades')
df = df.rename(columns={'Grades': 'Grade'})
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52