Home > front end >  How to split pairing text on pandas
How to split pairing text on pandas

Time:06-03

Here's my dataframe

Place               Pair
Boat Semeru          Andy.Johnson Mary.Smith, Adam.Roger Bella.Huri
Boat Merbabu         Sena.Batra Lera.Clara, Tracy.Roger Bella.Salsa, Iman.Veva Umi.Zuba

Here's my expected output

Place                Husband          Wife
Boat Semeru          Andy.Johnson     Mary.Smith
Boat Semeru          Adam.Roger       Bella.Huri
Boat Merbabu         Sena.Batra       Lera.Clara
Boat Merbabu         Tracy.Roger      Bella.Salsa
Boat Merbabu         Iman.Veva        Umi.Zuba

CodePudding user response:

split with explode

df['Pair'] = df['Pair'].str.split(', ')
df = df.explode('Pair',ignore_index=True)
df[['Husband','Wife']] = df.pop('Pair').str.split(' ').tolist()
df
Out[412]: 
          Place       Husband         Wife
0   Boat Semeru  Andy.Johnson   Mary.Smith
1   Boat Semeru    Adam.Roger   Bella.Huri
2  Boat Merbabu     ena.Batra   Lera.Clara
3  Boat Merbabu   Tracy.Roger  Bella.Salsa
4  Boat Merbabu     Iman.Veva     Umi.Zuba
  • Related