0

I have dataframe. Examples:

df1:
Date       Fruit                Color 
2013-11-23 Banana@Orange       Yellow
2013-11-24 Orange              Orange
2013-11-25 Apple@Strawberry     Green
2013-11-24 Celery               Green

I want to receive:

df1:
Date       Fruit                Color 
2013-11-23 Banana              Yellow
2013-11-23 Orange              Yellow
2013-11-24 Orange              Orange
2013-11-25 Apple                Green
2013-11-25 Strawberry           Green
2013-11-24 Celery               Green

My only idea is to go through the loop, find the delimiter and build a new array. How else can this be done?

Lionell
  • 69
  • 7
  • 3
    Does this answer your question? [Split cell into multiple rows in pandas dataframe](https://stackoverflow.com/questions/50731229/split-cell-into-multiple-rows-in-pandas-dataframe) – dsanatomy May 21 '20 at 18:08

1 Answers1

1

Use str.split and df.explode(pandas version >= 0.25):

In [1501]: df1.Fruit = df1.Fruit.str.split('@')
In [1503]: df1.explode('Fruit')               
Out[1503]: 
         Date       Fruit   Color
0  2013-11-23      Banana  Yellow
0  2013-11-23      Orange  Yellow
1  2013-11-24      Orange  Orange
2  2013-11-24       Apple   Green
2  2013-11-24  Strawberry   Green
3  2013-11-24      Celery   Green
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58