0

I have dataset looks like this

enter image description here

And I want the output of this data frame like this. So it's kind of one to one mapping of row values. Assume option1 and option2 has same comma separated values.

enter image description here

Please let me know how do I achieve this ?

bad_coder9042093
  • 307
  • 3
  • 13
  • Does this answer your question? [Split (explode) pandas dataframe string entry to separate rows](https://stackoverflow.com/questions/12680754/split-explode-pandas-dataframe-string-entry-to-separate-rows) – SomeDude Dec 01 '22 at 13:41

1 Answers1

0

You can use the zip() function from the standard Python library and the explode() method from Pandas dataframe like that :

df["option1"] = df["option1"].str.split(",")
df["option2"] = df["option2"].str.split(",")
df["option3"] = df["option3"]*max(df["option1"].str.len().max(), df["option2"].str.len().max())
new_df = pd.DataFrame(df.apply(lambda x: list(zip(x[0], x[1], x[2])), axis=1).explode().to_list(), columns=df.columns)
new_df
Pierre-Loic
  • 1,524
  • 1
  • 6
  • 12