如何扩展CSV表格冗余列?13列CSV数据组合扩展需求
处理CSV数据扩展:生成多字段组合记录
没问题,我来帮你搞定这个CSV数据扩展的需求!你要把每条13列的记录拆分成多种接近但不完全相同的有效组合,还要处理缺失数据的情况,对吧?咱们用Python来实现这个逻辑,毕竟它处理CSV和数据组合特别顺手。
思路梳理
首先咱们把字段分成几个逻辑组,每个组内的字段是同类型的可选值,然后生成这些组之间的笛卡尔积组合,同时自动跳过空值:
- 姓名组:
firstName/firstName2(名) +lastName/lastName2(姓),生成所有有效的名+姓搭配 - 位置组:
location1-location4,每个非空位置都作为独立可选值 - 邮箱组:
email/email2,每个非空邮箱作为独立可选值 - 电话组:
phone-phone3,每个非空电话作为独立可选值
这样每条原始记录就能扩展成「名×姓×位置×邮箱×电话」的所有有效组合(如果某个组全空,就留空处理)。
代码实现
下面是完整的Python代码,你可以直接运行,记得替换输入输出的CSV路径:
import csv from itertools import product def clean_field(value): # 清洗字段:去掉首尾空格,空字符串转为None cleaned = value.strip() if isinstance(value, str) else value return cleaned if cleaned else None def generate_combinations(record): # 提取每个组的非空值 first_names = [v for v in [record['firstName'], record['firstName2']] if clean_field(v) is not None] last_names = [v for v in [record['lastName'], record['lastName2']] if clean_field(v) is not None] locations = [v for v in [record['location1'], record['location2'], record['location3'], record['location4']] if clean_field(v) is not None] emails = [v for v in [record['email'], record['email2']] if clean_field(v) is not None] phones = [v for v in [record['phone'], record['phone2'], record['phone3']] if clean_field(v) is not None] # 如果名或姓全空,跳过这条记录的组合(根据需求可调整) if not first_names or not last_names: return [] # 生成笛卡尔积组合,处理空组的情况 # 给空组添加一个None,保证product能正常生成 locations = locations if locations else [None] emails = emails if emails else [None] phones = phones if phones else [None] combinations = [] for fn, ln, loc, eml, ph in product(first_names, last_names, locations, emails, phones): # 构造新记录,保留原始列结构,仅填充当前组合的字段,其他留空 new_record = { 'firstName': fn, 'firstName2': '', 'lastName': ln, 'lastName2': '', 'location1': loc if loc else '', 'location2': '', 'location3': '', 'location4': '', 'email': eml if eml else '', 'email2': '', 'phone': ph if ph else '', 'phone2': '', 'phone3': '' } combinations.append(new_record) # 去重(避免相同组合重复生成) unique_combinations = [dict(t) for t in {tuple(d.items()) for d in combinations}] return unique_combinations # 主流程 input_csv = 'input.csv' # 替换成你的输入文件路径 output_csv = 'expanded_output.csv' with open(input_csv, 'r', newline='', encoding='utf-8') as infile: reader = csv.DictReader(infile) fieldnames = reader.fieldnames with open(output_csv, 'w', newline='', encoding='utf-8') as outfile: writer = csv.DictWriter(outfile, fieldnames=fieldnames) writer.writeheader() for record in reader: # 生成当前记录的所有组合 expanded_records = generate_combinations(record) # 写入所有组合 for exp_record in expanded_records: writer.writerow(exp_record) print(f"扩展完成!结果已保存到 {output_csv}")
关键细节说明
- 数据清洗:
clean_field函数会自动去掉字段首尾空格,把空字符串转为None,避免无效的空值参与组合 - 空值处理:如果某个字段组全空(比如所有电话都为空),会用
None占位,保证组合能正常生成,最终输出时转为空字符串 - 去重处理:通过集合去重,避免因原始字段重复(比如
firstName和firstName2值相同)导致生成重复记录 - 列结构保留:扩展后的记录依然保持原始13列的结构,仅填充当前组合的字段,其他列留空(你可以根据需求调整这部分,比如保留原始其他字段的值)
自定义调整建议
如果你不想生成所有笛卡尔积,只想部分组合(比如每个记录只生成「名+姓」搭配任意一个位置/邮箱/电话),可以修改product的参数;如果希望保留原始记录的其他非组合字段,直接在new_record里把对应字段设为record['字段名']即可。
内容的提问来源于stack exchange,提问作者mas
相关产品推荐
相关产品推荐

