0

I have dataframe that looks like this. How can I separate the parameter ReformZoneCode to different rows?

        ReformZoneCode   value  name
0                  210  141       a
1      210;221;222;223  142       b
2  210;221;222;231;234  143       c
3  210;222;223;232;233  144       d
4          221;231;234  145       e

like

  ReformZoneCode   value  name   
     0 210  141   a
     1 210  142   b
     2 221  142   b
     3 222  142   b 
     4 223  142   b
     5 210  143   c ...
sloshy
  • 29
  • 4
rafine
  • 361
  • 3
  • 18
  • 1
    potential duplicate here https://stackoverflow.com/questions/50731229/split-cell-into-multiple-rows-in-pandas-dataframe – sophocles Mar 02 '21 at 14:12

1 Answers1

1

Use str.split to split the code, then explode:

(df.assign(ReformZoneCode=df.ReformZoneCode.str.split(';'))
   .explode('ReformZoneCode')
)

Output:

  ReformZoneCode  value name
0            210    141    a
1            210    142    b
1            221    142    b
1            222    142    b
1            223    142    b
2            210    143    c
2            221    143    c
2            222    143    c
2            231    143    c
2            234    143    c
3            210    144    d
3            222    144    d
3            223    144    d
3            232    144    d
3            233    144    d
4            221    145    e
4            231    145    e
4            234    145    e
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74