0

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
Kevin Nash
  • 1,511
  • 3
  • 18
  • 37
  • 1
    so you can use `m=df.apply(lambda x:x.str.split('//'))` and then `unnesting(m,['fruits', 'quantity', 'price'])`, `unnesting` from [here](https://stackoverflow.com/questions/53218931/how-to-unnest-explode-a-column-in-a-pandas-dataframe/53218939#53218939) , let me know if you have any doubts – anky Jun 23 '19 at 09:17
  • @anky_91, thanks for the share. I went through that post and was able to modify again for one column but I am not sure how to extend it to multiple columns. I have edited my initial post with my updated code.. – Kevin Nash Jun 23 '19 at 15:48
  • 1
    use the unnesting function which you will down below the accepted ans. :) it takes a list of cols as a param – anky Jun 23 '19 at 15:49

0 Answers0