Python 如何一次为多个列填充Nan值?
在上面的示例数据框中,我有Nan值,需要填充 它包含一次全部向前填充,我使用的代码是:Python 如何一次为多个列填充Nan值?,python,python-3.x,pandas,fillna,Python,Python 3.x,Pandas,Fillna,在上面的示例数据框中,我有Nan值,需要填充 它包含一次全部向前填充,我使用的代码是: df Fruits Veg Non_veg 1 Apple Broccoli Chicken 2 Banana Nan Nan 3 Nan Tomato Nan 这种编码方式正确吗?另外,如果前向填充单元具有与上述数据帧类似的另一个Nan值,如何克服 ffill的预期输出为: df[['Fruits','Veg','Non_veg']].fil
df
Fruits Veg Non_veg
1 Apple Broccoli Chicken
2 Banana Nan Nan
3 Nan Tomato Nan
这种编码方式正确吗?另外,如果前向填充单元具有与上述数据帧类似的另一个Nan值,如何克服
ffill的预期输出为:
df[['Fruits','Veg','Non_veg']].fillna(method='ffill',inplace=True)
用法:。您可以在此处了解如何在本地使用:
不要在切片对象上使用
in place
。分配
#df.replace('Nan',np.nan) #if NaN is string
cols=['Fruits','Veg','Non_veg']
df[cols]=df[cols].ffill()
您应该删除
inplace=True
df['Fruits','Veg','Non_-Veg']=df['Fruits','Veg','Non_-Veg'].fillna(方法='ffill')
请显示您的预期输出
#df.replace('Nan',np.nan) #if NaN is string
cols=['Fruits','Veg','Non_veg']
df[cols]=df[cols].ffill()
df.loc[:, ['Fruits','Veg','Non_veg']] = df.loc[:, ['Fruits','Veg','Non_veg']].ffill()