0

I have a Dataframe as shown Below, I have to duplicate rows using the comma as a delimiter. It's easier to understand once you see the dataframes below!:

ID      Fruit
10000   Apple, Orange, Pear
10001   Apple, Banana

I want to Dataframe below:

ID      Fruit
10000   Apple 
10000   Orange
10000   Pear
10001   Apple 
10001   Banana
4

2 Answers 2

1

Try:

df['Fruit']=df['Fruit'].str.split(", ")
df=df.explode('Fruit')

Outputs:

      ID   Fruit
0  10000   Apple
0  10000  Orange
0  10000    Pear
1  10001   Apple
1  10001  Banana
Sign up to request clarification or add additional context in comments.

Comments

0

If df looks like this:

>>> df = pd.DataFrame({'ID': [10000, 10001], 'Fruit': ['Apple, Orange, Pear', 'Apple, Banana']})
>>> print(df)
      ID                Fruit
0  10000  Apple, Orange, Pear
1  10001        Apple, Banana

you can use the pandas.DataFrame.apply() method to make a new column of lists consisting of dictionaries with new rows. And after that, you can concatenate these lists in order to make a new data frame out of them. The code is following:

>>> df['new'] = df.apply(lambda row: [{'ID': row.ID, 'Fruit': item} for item in row.Fruit.split(', ')], axis=1)
>>> df_new = pd.DataFrame(df.new.sum())
>>> print(df_new)
      ID   Fruit
0  10000   Apple
1  10000  Orange
2  10000    Pear
3  10001   Apple
4  10001  Banana

2 Comments

Thanks, that's really useful.
That's bad approach- you should rather avoid using .apply(...), unless you really have to: stackoverflow.com/a/54432584/11610186

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.