gpt4 book ai didi

python - 在 pandas 数据框列中前向填充缺失值的有效解决方案?

转载 作者:行者123 更新时间:2023-11-28 22:29:21 25 4
gpt4 key购买 nike

我需要转发组内数据框列中的填充值。我应该注意到,组中的第一个值永远不会因构造而丢失。我目前有以下解决方案。

df = pd.DataFrame({'a': [1,1,2,2,2], 'b': [1, np.nan, 2, np.nan, np.nan]})

# desired output
a b
1 1
1 1
2 2
2 2
2 2

这是迄今为止我尝试过的三种解决方案。

# really slow solutions
df['b'] = df.groupby('a')['b'].transform(lambda x: x.fillna(method='ffill'))
df['b'] = df.groupby('a')['b'].fillna(method='ffill')

# much faster solution, but more memory intensive and ugly all around
tmp = df.drop_duplicates('a', keep='first')
df.drop('b', inplace=True, axis=1)
df = df.merge(tmp, on='a')

所有这三个都产生了我想要的输出,但前两个在我的数据集上花费了很长时间,第三个解决方案占用更多内存并且感觉相当笨重。还有其他方法可以转发填充列吗?

最佳答案

您需要按两列 df.sort_values(['a', 'b']).ffill() 进行排序以确保稳健性。如果 np.nan 留在组内的第一个位置,ffill 将用前一组的值填充它。因为 np.nan 将放在任何排序的末尾,所以按 ab 排序可确保您不会有 np.nan 在任何组的前面。然后,您可以使用初始索引 .loc.reindex 来取回您的原始订单。

这显然会比其他提议慢一点...但是,我认为它是正确而其他提议则不是。

演示

考虑数据框 df

df = pd.DataFrame({'a': [1,1,2,2,2], 'b': [1, np.nan, np.nan, 2, np.nan]})

print(df)

a b
0 1 1.0
1 1 NaN
2 2 NaN
3 2 2.0
4 2 NaN

尝试

df.sort_values('a').ffill()

a b
0 1 1.0
1 1 1.0
2 2 1.0 # <--- this is incorrect
3 2 2.0
4 2 2.0

改为做

df.sort_values(['a', 'b']).ffill().loc[df.index]

a b
0 1 1.0
1 1 1.0
2 2 2.0
3 2 2.0
4 2 2.0

特别说明
如果整个组都有缺失值,这仍然是不正确的

关于python - 在 pandas 数据框列中前向填充缺失值的有效解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43075747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com