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

Pandas DataFrame中利用多列非零值批量替换指定列值的高效方法

Efficiently Replace Column Values with Non-Zero Values from Other Columns

Nice question! When you're dealing with 100+ columns, looping through each one manually is definitely not the most efficient approach—let's use pandas' vectorized operations to solve this cleanly and quickly, while preserving the priority logic you used (later columns overwrite earlier ones when multiple non-zero values exist).

Step-by-Step Solution

First, let's recap your sample data for context:

import pandas as pd
import numpy as np

d = {'x': [1, 2, 3], 'y': ["", "", 4], 'z': ["", 5, ""]}
df = pd.DataFrame(index=['A','B','C'], data=d)
df.replace('', 0, inplace=True)

Here's the efficient, scalable method:

# 1. Identify all columns we need to check (exclude the target column 'x')
cols_to_check = df.columns.drop('x')

# 2. Convert 0s to NaN so we can ignore them in fill operations
temp_df = df[cols_to_check].replace(0, pd.NA)

# 3. Get the rightmost non-zero value per row (matches your original priority: z > y)
#    Use ffill(axis=1).iloc[:, 0] if you want leftmost columns to have higher priority
replacement_values = temp_df.bfill(axis=1).iloc[:, -1]

# 4. Replace 'x' with the non-zero values where available, keep original 'x' otherwise
df['x'] = replacement_values.fillna(df['x'])

What This Does

  • cols_to_check: Dynamically selects all columns except x, so you don't have to hardcode 100 column names.
  • temp_df.replace(0, pd.NA): Turns zeros into missing values, so our fill logic only considers valid non-zero entries.
  • bfill(axis=1): Fills missing values row-wise from right to left. Taking the last column (iloc[:, -1]) gives us the rightmost non-zero value per row—exactly matching your original code where z overwrites y if both are non-zero.
  • fillna(df['x']): Keeps the original x value for rows where all checked columns are zero.

Verify the Result

Running this code on your sample data produces exactly the output you wanted:

x  y  z
A  1  0  0
B  5  0  5
C  4  4  0

Adjusting Priority

If you want leftmost columns to take precedence (e.g., y overwrites z), swap out the replacement line with:

replacement_values = temp_df.ffill(axis=1).iloc[:, 0]

This approach leverages pandas' optimized vectorized operations, so it will handle your 100-column DataFrame far faster than looping through each column individually.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:20:52