1

There is one column in my pandas dataframe with more information. I need to split this to different rows based on a punctuation in the values in the column. Could anyone please help me solve this.

What I have:

ProductId   Icecream                  State    Country
1           Vanilla;Strawberry        TN       IN
2           Blueberry;Vanilla;Mango   KL       IN

Expected Output:

ProductId   Icecream                  State    Country
1           Vanilla                   TN       IN
1           Strawberry                TN       IN
2           Blueberry                 KL       IN
2           Vanilla                   KL       IN
2           Mango                     KL       IN

1 Answers1

3

Use str.split and explode:

>>> df1.assign(Icecream=df1['Icecream'].str.split(';')).explode('Icecream')

   ProductId    Icecream State Country
0          1     Vanilla    TN      IN
0          1  Strawberry    TN      IN
1          2   Blueberry    KL      IN
1          2     Vanilla    KL      IN
1          2       Mango    KL      IN
Corralien
  • 70,617
  • 7
  • 16
  • 36