如何在Pandas中基于idx列值,用单个apply语句生成多列?
解决方案
方法1:单个apply语句生成多列
可以通过apply逐行处理并返回包含目标列的Series,一次性生成所有需要的列。考虑到你提到的不同行列表长度不同的情况,加入索引越界判断避免报错:
import pandas as pd import random # 生成适配不同列表长度的测试数据 nrows = 5 a = [[random.randint(0, 10) for _ in range(random.randint(1,4))] for i in range(nrows)] b = [[random.randint(0, 10) for _ in range(random.randint(1,4))] for i in range(nrows)] c = [[random.randint(0, 10) for _ in range(random.randint(1,4))] for i in range(nrows)] idx = [random.randint(0, 3) for i in range(nrows)] df = pd.DataFrame({'a': a, 'b': b, 'c': c, 'idx': idx}) # 单个apply生成三列 df[['a_des', 'b_des', 'c_des']] = df.apply( lambda row: pd.Series( [ row['a'][row['idx']] if row['idx'] < len(row['a']) else None, row['b'][row['idx']] if row['idx'] < len(row['b']) else None, row['c'][row['idx']] if row['idx'] < len(row['c']) else None ], index=['a_des', 'b_des', 'c_des'] ), axis=1 )
方法2:更高效的批量遍历(推荐)
apply是逐行遍历,数据量大时效率较低。用循环结合列表推导式能大幅提升速度,且扩展性更强——新增列只需在列表中添加对应列名:
# 定义源列和目标列的对应关系 source_cols = ['a', 'b', 'c'] # 批量生成目标列 for col in source_cols: df[f'{col}_des'] = [lst[i] if i < len(lst) else None for lst, i in zip(df[col], df['idx'])]
方法3:封装函数复用逻辑
如果需要更清晰的代码结构,可将提取逻辑封装成函数,再批量调用:
def extract_element(col_name, df): return [lst[i] if i < len(lst) else None for lst, i in zip(df[col_name], df['idx'])] # 批量生成目标列 source_cols = ['a', 'b', 'c'] for col in source_cols: df[f'{col}_des'] = extract_element(col, df)
内容的提问来源于stack exchange,提问作者DeltaIV
相关产品推荐
相关产品推荐

