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

在Pandas中向列内的列表追加值的实现方法

实现DataFrame中c列值追加至notes列列表的方法

核心方案

针对需求,有两种高效的实现方式:

  • 方法一:使用apply结合lambda函数
    适合小体量DataFrame,代码直观易读:

    import pandas as pd
    
    # 假设你的DataFrame变量名为df
    df['notes'] = df.apply(lambda row: row['notes'] + [row['c']], axis=1)
    
  • 方法二:列表推导式
    处理大数据量时性能更优,避免apply的循环开销:

    df['notes'] = [notes_list + [c_val] for notes_list, c_val in zip(df['notes'], df['c'])]
    

效果验证

假设原始数据结构如下:

abcnotes
123['x', 'y']
456['p', 'q', 'r']

执行上述任意一种方法后,得到预期结果:

abcnotes
123['x', 'y', 3]
456['p', 'q', 'r', 6]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 00:24:18