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

Pandas使用字典映射新增列时如何将列表值拆分为多个列

方案1:直接拆分映射后的列表

你已经通过map得到了存储列表的序列,直接用pandas的.str访问器按索引取值即可,代码最简单:

df1['col1'] = df1['date'].map(df_dict).str[0]
df1['col2'] = df1['date'].map(df_dict).str[1]

方案2:一次映射直接生成两列

避免两次执行map操作,数据量大时效率更高:

df1[['col1', 'col2']] = df1['date'].map(df_dict).apply(pd.Series)

方案3:直接用DataFrame关联(更推荐)

不需要提前构造df_dict,用pandas原生的merge操作逻辑更清晰,不容易出错:

# 直接关联两个表,按年份匹配
df1 = df1.merge(df, left_on='date', right_on='Year', how='left')
# 重命名列并保留需要的字段
df1 = df1.rename(columns={'DST Start':'col1', 'DST End':'col2'})[['date', 'col1', 'col2']]

以上三种方法都可以得到你预期的输出结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 02:30:04