[英]ffill not filling data in pandas dataframe
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
I am trying for forward filling using ffill.我正在尝试使用 ffill 进行正向填充。 It is not working
它不工作
cols = df.columns[:4].tolist()
df[cols] = df[cols].ffill()
I also tried :我也试过:
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)
确切数据不同(包含字符串、数字和空列)
desired o/p:期望的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
替换列子集中的空值:
df[cols] = df[cols].replace('', np.nan).ffill()
You should replace the empty strings with np.NaN
before:您应该在之前用
np.NaN
替换空字符串:
df = df.replace('', np.NaN)
df[cols] = df[cols].ffill()
Replace ''
with np.nan
first:先用
np.nan
替换''
:
df[df='']=np.nan
df[cols] = df[cols].ffill()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.