I am trying to convert Dataframe that has multiple values in the same row into multiple rows. Given below is how my sample data looks like:
import pandas as pd
df = pd.DataFrame({ 'fruits':["apples",
"bananas // oranges",
"kiwi",
"pears // bananas"], 'quantity':[24,5//34,2,26/5], 'price':[12,90 // 30, 64, 87 // 12]})
Output:
fruits quantity price
0 apples 24 12
1 bananas // oranges 5 // 34 90 //30
2 kiwi 2 64
3 pears // bananas 26 // 5 87 // 12
Expected output:
fruits quantity price
0 apples 24 12
1 bananas 5 90
2 oranges 34 30
3 kiwi 2 64
4 pears 26 87
5 bananas 5 12
Based on reading several posts, I was able to perform the below:
df = df.join(pd.DataFrame(df.'fruits'.str.split(',', expand=True).stack().reset_index(level=2, drop=True)
,columns=['fruits_')).drop('fruits',1).rename(columns=str.strip).reset_index(drop=True)
The above works well if I want to explode one column in the Dataframe, but I am not sure how to do it for multiple columns as in my case.
Update :
df = df.set_index(['fruits'])
df = df.astype(str) + '// '
df = df['quantity'].str.split('//', expand=True).stack().reset_index(-1, drop=True).replace(' ', np.nan).dropna().reset_index()
df