I have dataset looks like this
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.
Please let me know how do I achieve this ?
I have dataset looks like this
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.
Please let me know how do I achieve this ?
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