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

如何用Pandas将指定列值填充至0值列?代码无效果求助

问题解决:替换DataFrame中指定列的0值

你的代码未生效有两个核心问题:

  • 链式索引(df_result[condition1]['유통시총(조)'])会返回DataFrame的副本,修改副本不会影响原数据。
  • 第二个操作里列名写错了,把目标列「유통량(백만)」误写成了「유통시총(백만)」。

正确实现代码

用.loc直接定位原DataFrame的行和列,确保修改作用到原数据:

# 条件定义
condition1 = df_result['유통시총(조)'] == 0
condition2 = df_result['유통량(백만)'] == 0

# 替换0值
df_result.loc[condition1, '유통시총(조)'] = df_result.loc[condition1, 'self_reported_market_cap']
df_result.loc[condition2, '유통량(백만)'] = df_result.loc[condition2, 'self_reported_circulating_supply']

补充:更简洁的写法

也可以用np.where一次性完成替换,代码更紧凑:

import numpy as np

df_result['유통시총(조)'] = np.where(df_result['유통시총(조)'] == 0, df_result['self_reported_market_cap'], df_result['유통시총(조)'])
df_result['유통량(백만)'] = np.where(df_result['유통량(백만)'] == 0, df_result['self_reported_circulating_supply'], df_result['유통량(백만)'])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 03:35:14