Home > Software design >  How to convert the data separated by ',' into the next row? (Pandas)
How to convert the data separated by ',' into the next row? (Pandas)

Time:10-30

I have a dataframe:

Col 1   Col 2

A,B,C   Group1
D,E,F   Group2
G,H,I   Group3

I want to separate the data which has , sign into new rows.

Expected Output:

Col1 Col2
A    Group1
B    Group1
C    Group1
D    Group2
E    Group2
F    Group2
G    Group3
H    Group3
I    Group3

CodePudding user response:

Let us try

df = df.join(df.pop('Col 1').str.split(',').explode())
df
Out[210]: 
    Col 2 Col 1
0  Group1    A
0  Group1    B
0  Group1    C
1  Group2    D
1  Group2    E
1  Group2    F
2  Group3    G
2  Group3    H
2  Group3    I
  • Related