I have to below dataframe:
import pandas as pd
a = pd.DataFrame([{"name": "John",
"item" : "item1||item2||item3",
"itemVal" : "item1Val||item2Val||item3Val"},
{"name" : "Tom",
"item":"item4",
"itemVal" : "item4Val"
}
])
The dataframe is like this:
name item itemVal
John item1||item2||item3 item1Val||item2Val||item3Val
Tom item4 item4Val
I want to explode the row into multiple rows so that it will be like this (note that the item
and its itemVal
need to match).
name item itemVal
John item1 item1Val
John item2 item2Val
John item3 item3Val
Tom item4 item4Val
I have looked at other answers here:
Split (explode) pandas dataframe string entry to separate rows
pandas: How do I split text in a column into multiple rows?
But the works on only one column. How do I make it work on multiple columns? I'm using Pandas 1.0.1 and Python 3.8