填充不填充 pandas 数据框中的数据 [英] ffill not filling data in pandas dataframe
本文介绍了填充不填充 pandas 数据框中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的数据框:
I have a dataframe like this :
A B C E D
---------------
0 a r g g
1 x
2 x f f r
3 t
3 y
我正在尝试使用 ffill 进行正向填充.它不起作用
I am trying for forward filling using ffill. It is not working
cols = df.columns[:4].tolist()
df[cols] = df[cols].ffill()
我也试过:
df[cols] = df[cols].fillna(method='ffill')
但它没有被填满.是数据中的空列导致了这个问题吗?
But it is not getting filled. Is it the empty columns in data causing this issue?
数据被模拟.确切数据不同(包含字符串、数字和空列)
Data is mocked. Exact data is different (contains strings,numbers and empty columns)
期望的o/p:
A B C E D
---------------
0 a r g g
1 a r g x
2 x f f r
3 x f f t
3 x f f y
推荐答案
用 NaN
替换列子集中的空值:
Replace empty values in subset of columns by NaN
:
df[cols] = df[cols].replace('', np.nan).ffill()
这篇关于填充不填充 pandas 数据框中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文