在数据处理中,我们经常需要根据一列的特定条件来创建或修改另一列。更复杂的情况是,当条件不满足时,新列的值需要依赖于其上方或下方的某个有效值。例如,给定一个包含“colonne 1”和“dimension 1”的dataframe,我们的目标是创建一个新列“new”,其规则如下:
传统的迭代方法(如使用for循环或apply函数)在处理大型DataFrame时效率低下。Pandas提供了更优化的矢量化操作来解决这类问题。
Pandas中的Series.where()方法允许我们根据一个布尔条件选择性地替换Series中的值。当条件为False时,对应位置的值会被替换为NaN(默认),这为后续的填充操作创造了条件。接着,我们可以利用Series.bfill()(backward fill,向后填充)或Series.ffill()(forward fill,向前填充)来处理这些NaN值,实现复杂的依赖填充逻辑。
首先,我们使用where()方法来初步填充“new”列。如果“Dimension 1”是“Organisation”,则取“Colonne 1”的值;否则,该位置暂时留空(NaN)。
import pandas as pd import io # 示例数据 data = """ Colonne 1 Dimension 1 0 MTN_LI2 Indicator 1 MTN_IRU Indicator 2 MTN_ACE Indicator 3 MTN_IME Indicator 4 RIPP7 Organisation 5 CA_SOT Indicator 6 CA_OTI Indicator 7 CNW00 Organisation 8 BSNTF Organisation 9 RIPNJ Organisation """ df = pd.read_csv(io.StringIO(data), sep=r'\s{2,}', engine='python', skipinitialspace=True) # 步骤1: 使用where()初步创建新列 # 如果 df['Dimension 1'] 等于 'Organisation',则取 df['Colonne 1'] 的值, # 否则,该位置为 NaN df['new_temp'] = df['Colonne 1'].where(df['Dimension 1'].eq('Organisation')) print("--- 步骤1结果 (初步填充,非Organisation为NaN) ---") print(df)
输出 df['new_temp'] 将会是:
--- 步骤1结果 (初步填充,非Organisation为NaN) --- Colonne 1 Dimension 1 new_temp 0 MTN_LI2 Indicator NaN 1 MTN_IRU Indicator NaN 2 MTN_ACE Indicator NaN 3 MTN_IME Indicator NaN 4 RIPP7 Organisation RIPP7 5 CA_SOT Indicator NaN 6 CA_OTI Indicator NaN 7 CNW00 Organisation CNW00 8 BSNTF Organisation BSNTF 9 RIPNJ Organisation RIPNJ
为了满足“如果不是'Organisation',则取其下方最近的'Organisation'值”的需求,我们可以使用bfill()。bfill()会从Series的末尾开始,用遇到的第一个非NaN值填充其上方的所有NaN。
# 步骤2: 使用 bfill() 填充 NaN df['new_bfill'] = df['new_temp'].bfill() print("\n--- 步骤2结果 (使用 bfill() 填充) ---") print(df[['Colonne 1', 'Dimension 1', 'new_bfill']])
输出结果:
--- 步骤2结果 (使用 bfill() 填充) --- Colonne 1 Dimension 1 new_bfill 0 MTN_LI2 Indicator RIPP7 1 MTN_IRU Indicator RIPP7 2 MTN_ACE Indicator RIPP7 3 MTN_IME Indicator RIPP7 4 RIPP7 Organisation RIPP7 5 CA_SOT Indicator CNW00 6 CA_OTI Indicator CNW00 7 CNW00 Organisation CNW00 8 BSNTF Organisation BSNTF 9 RIPNJ Organisation RIPNJ
这完美地解决了最初的问题描述:“如果Dimension 1不等于Organisation,则该单元格获取其上方(按逻辑,是其下方最近的Organisation值向上填充)的单元格值。”
作为对比,如果需求是“如果不是'Organisation',则取其上方最近的'Organisation'值”,那么可以使用ffill()。ffill()会从Series的开头开始,用遇到的第一个非NaN值填充其下方的所有NaN。
# 步骤3: 使用 ffill() 填充 NaN (作为对比,解决不同需求) df['new_ffill'] = df['new_temp'].ffill() print("\n--- 步骤3结果 (使用 ffill() 填充) ---") print(df[['Colonne 1', 'Dimension 1', 'new_ffill']])
输出结果:
--- 步骤3结果 (使用 ffill() 填充) --- Colonne 1 Dimension 1 new_ffill 0 MTN_LI2 Indicator NaN 1 MTN_IRU Indicator NaN 2 MTN_ACE Indicator NaN 3 MTN_IME Indicator NaN 4 RIPP7 Organisation RIPP7 5 CA_SOT Indicator RIPP7 6 CA_OTI Indicator RIPP7 7 CNW00 Organisation CNW00 8 BSNTF Organisation BSNTF 9 RIPNJ Organisation RIPNJ
注意,ffill()在遇到第一个Organisation值(RIPP7)之前,无法填充前几个NaN,因为它们上方没有有效值。这说明了ffill()和bfill()在处理起始/结束NaN时的不同行为。
通过掌握where()和填充方法,您可以高效且优雅地解决Pandas中涉及复杂条件和行间依赖的数据转换问题。
以上就是利用Pandas高效创建依赖上一个有效值的条件列的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号