I have the following pandas dataframe:
In:
df
out:
A B C D
0 0938320 usa amazon orange: $ 8.00| pineapple: $2.00
1 0938320 usa alibaba orange: $ 8.00| apple: $2.00
2 0938320 usa ebay mint: $ 8.00| watermelon: $2.00| mint: $2.00
...
n 0938320 usa amazon pear: $ 8.00| bannana: $2.00
I would like to split by | and stack it into (*):
A B C D
0 0938320 usa amazon orange: $ 8.00
1 0938320 usa amazon pineapple: $2.00
2 0938320 usa alibaba orange: $ 8.00
3 0938320 usa alibaba apple: $2.00
4 0938320 usa bay mint: $ 8.00
5 0938320 usa ebay watermelon: $2.00
6 0938320 usa ebay mint: $2.00
7 0938320 usa amazon pear: $ 8.00
...
8 0938320 usa amazon bannana: $2.00
So, I tried the following:
In:
s = df2.D.str.split("|").apply(pd.Series, 1).stack()
s.index = s.index.droplevel(-1)
del df2['D']
df.join(s)
out:
ValueError: Other Series must have a name
And:
b = pd.DataFrame(df2.D.str.split('|').tolist(), index=df2['A','B','C']).stack()
b = b.reset_index()[[0, 'D']]
b.columns = ['A','B','C']
b
However, is not working. How can I modify the last approach in order to get (*)?. I guess that my main problem is that I do not know how to take all the columns in index=df2['A','B','C']).stack().