0

I have a dataframe that looks like this:


ID Number   Description Code             Total Cost     Store ID

A33          Ice cream; Chocolate           20           5
B44          Chips; Milk                    15           6
C66          Cheese; Ice cream              10           6
V77          Pasta; Rice                    8            8

I want to split the value of the Description Code into two, generating a new row based on the ";" symbol.

The output should be like this

ID Number   Description Code   Total Cost     Store ID

A33          Ice cream            20           5
A33          Chocolate            20           5
B44           Chips               15           6
B44           Milk                15           6
C66          Cheese               10           6
C66          Ice cream            10           6
V77          Pasta                8            8
V77          Rice                 8            8

nrod88
  • 25
  • 5

1 Answers1

0

Use str.split to create a list of Description Code then explode your dataframe:

out = df.assign(**{'Description Code': lambda x: x['Description Code'].str.split('; ')}) \
        .explode('Description Code', ignore_index=True)
print(out)

# Output
  ID Number Description Code  Total Cost  Store ID
0       A33        Ice cream          20         5
1       A33        Chocolate          20         5
2       B44            Chips          15         6
3       B44             Milk          15         6
4       C66           Cheese          10         6
5       C66        Ice cream          10         6
6       V77            Pasta           8         8
7       V77             Rice           8         8

Note: As Description Code is not a valid python identifier you have to use **{k1: v1, k2: v2} to expand parameters.

Corralien
  • 109,409
  • 8
  • 28
  • 52