0

How can I get an expanded version of a dataframe which has lists as values in it?

Here's a sample of the dataframe I have:

raw = pd.DataFrame().assign(Therapuetic_Area = ['Oncology'],
LocationState = [['Ohio','Illinois','Oregon','New York']])

Output of the raw dataframe

Now, I need it to look like this edited DataFrame:

edited = pd.DataFrame().assign(Therapuetic_Area = ['Oncology','Oncology','Oncology','Oncology'],LocationState = ['Ohio','Illinois','Oregon','New York'])

output of edited dataframe

Is there a Pandas method I can use for this? How could I get the edited dataframe without having to manually input the values? I can't possibly manually input it because my data is enormously large. Any help would be appreciated!

Luke
  • 23
  • 4

1 Answers1

1

you can use explode to create rows from the list values

raw.explode('LocationState')
Therapuetic_Area    LocationState
0   Oncology    Ohio
0   Oncology    Illinois
0   Oncology    Oregon
0   Oncology    New York
Naveed
  • 11,495
  • 2
  • 14
  • 21