You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pandas中拆分列并根据拆分值添加多行?

问题描述

现有如下结构的Pandas DataFrame(完整数据见下方代码):

{'last_name': {0: 'Acosta-Arriola',
  1: 'Afragola',
  10: 'Duntz'},
 'first_name_or_initial': {0: 'Jose',
  1: 'Sarah',
  10: 'Shawn'},
 'middle_name_or_initial': {0: 'Lusi;Luis',
  1: 'R.;B.',
  10: 'R.;Richard'},
 'Suffix': {0: '',
  1: '',
  10: ''},
 'address_1': {0: '',
  1: '51 Indigo Trail',
  10: '118 Brookside Avenue;9886 171 Street Place'}}

需要将middle_name_or_initial列按分号;拆分,每个拆分出的中间名对应生成一行新数据,其他列内容保持不变。例如原数据行:

Duntz   Shawn   R.;Richard 118 Brookside Avenue;9886 171 Street Place

需转换为:

1. Duntz - Shawn - R. - 118 Brookside Avenue;9886 171 Street Place
2. Duntz - Shawn - Richard - 118 Brookside Avenue;9886 171 Street Place
解决方案

使用Pandas的str.split()拆分目标列,再通过explode()将拆分后的列表展开为多行,最后按需格式化输出。

步骤1:数据加载与拆分展开

import pandas as pd

# 构造完整DataFrame
data = {'last_name': {0: 'Acosta-Arriola',
  1: 'Afragola',
  2: 'Bertolini',
  3: 'Coyle',
  4: 'Davis',
  10: 'Duntz',
  11: 'Eastman',
  12: 'Fitzgerald',
  13: 'Fitzgerald',
  14: 'Freeman',
  15: 'Freeman',
  16: 'Gambardella',
  17: 'Kelleher',
  18: 'King',
  19: 'Looney',
  20: 'Mccann',
  21: 'Murray',
  22: 'Palmeri',
  23: 'Powers',
  24: 'Vitelli',
  25: 'Wyzykowski'},
 'first_name_or_initial': {0: 'Jose',
  1: 'Sarah',
  2: 'Peter',
  3: 'James',
  4: 'Albert',
  10: 'Shawn',
  11: 'Bryan',
  12: 'Richard',
  13: 'Richard',
  14: 'Matthew',
  15: 'Matthew',
  16: 'Vincent',
  17: 'Robert',
  18: 'Thomas',
  19: 'Ray',
  20: 'Joseph',
  21: 'Joshua',
  22: 'Randy',
  23: 'Dennis',
  24: 'Robert',
  25: 'John'},
 'middle_name_or_initial': {0: 'Lusi;Luis',
  1: 'R.;B.',
  2: 'M.;Mario',
  3: 'M.;Michael',
  4: 'Chadbourne;C.',
  10: 'R.;Richard',
  11: 'J.;James',
  12: 'M.;J.;Micha',
  13: 'M.;Michael',
  14: 'Christopher;Robert',
  15: 'Christopher;C.',
  16: 'A.;Anthony',
  17: 'S.;Steven',
  18: 'E.;Emory',
  19: 'S.;Scott',
  20: 'M.;Michael',
  21: 'M.;P.',
  22: 'T.;Thomas',
  23: 'E.;Edward',
  24: 'J.;D.',
  25: 'J.;James'},
 'Suffix': {0: '',
  1: '',
  2: '',
  3: '',
  4: 'Jr.',
  10: '',
  11: '',
  12: '',
  13: '',
  14: '',
  15: '',
  16: '',
  17: '',
  18: 'Jr.',
  19: 'Jr.',
  20: '',
  21: '',
  22: '',
  23: 'Jr.',
  24: '',
  25: ''},
 'address_1': {0: '',
  1: '51 Indigo Trail',
  2: '90 Cherry Street;1295 Great Hill Road;90 Cherry Street',
  3: '51 Canary Court;51 Canary Court;687 Main Street',
  4: '39 Hemenway Street',
  10: '118 Brookside Avenue;9886 171 Street Place',
  11: '616 East Main Street;989 Boston Post Road;38 Mallard Court;1421 Naugatuck Avenue',
  12: '',
  13: '18 Fox Ridge Lane;18 Fox Ridge;18 Fox Ridge Road',
  14: '',
  15: '',
  16: '45 Jakobs Landing',
  17: '171 Williams Road;181 Knob Hill Road',
  18: '31 Millwood Drive;31 Millwood Drive;41 Waverly Park Road;31 Millwood Drive;25 Crouch Road',
  19: '',
  20: '17 Pheasant Run;25 Mcdermott Road;PO Box 510;17 Pheasant Run',
  21: '42 Seymour Street;42 Seymour Stt',
  22: '205 Mccall Road',
  23: '204 Milton Avenue;187 Milton Avenue',
  24: '16 Montgomery Drive',
  25: '457 Hill Street;139 County Line Road'}}

df = pd.DataFrame(data)

# 拆分中间名列并展开为多行
df_expanded = df.assign(middle_name_or_initial=df['middle_name_or_initial'].str.split(';')).explode('middle_name_or_initial')

步骤2:格式化输出结果

如果需要按示例格式输出,可遍历处理后的DataFrame:

# 按要求格式输出每一行
for idx, row in enumerate(df_expanded.itertuples(index=False), 1):
    suffix_segment = f' - {row.Suffix}' if row.Suffix else ''
    print(f"{idx}. {row.last_name} - {row.first_name_or_initial} - {row.middle_name_or_initial}{suffix_segment} - {row.address_1}")

效果示例

针对原数据中的Duntz行,处理后输出:

1. Duntz - Shawn - R. - 118 Brookside Avenue;9886 171 Street Place
2. Duntz - Shawn - Richard - 118 Brookside Avenue;9886 171 Street Place

内容的提问来源于stack exchange,提问作者myamulla_ciencia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 23:19:00