![](/img/trans.png)
[英]PANDAS (poputating datetime and ffill() the data in dataframe in pandas)
[英]ffill not filling data in pandas dataframe
我有一個這樣的數據框:
A B C E D
---------------
0 a r g g
1 x
2 x f f r
3 t
3 y
我正在嘗試使用 ffill 進行正向填充。 它不工作
cols = df.columns[:4].tolist()
df[cols] = df[cols].ffill()
我也試過:
df[cols] = df[cols].fillna(method='ffill')
但它並沒有被填滿。 是數據中的空列導致了這個問題嗎?
數據被嘲笑。 確切數據不同(包含字符串、數字和空列)
期望的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()
您應該在之前用np.NaN
替換空字符串:
df = df.replace('', np.NaN)
df[cols] = df[cols].ffill()
先用np.nan
替換''
:
df[df='']=np.nan
df[cols] = df[cols].ffill()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.