如何拆分 pandas 上的配对文本
How to split pairing text on pandas
这是我的数据框
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
这是我的预期输出
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
split
与 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
这是我的数据框
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
这是我的预期输出
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
split
与 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