Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何一次为多个列填充Nan值?_Python_Python 3.x_Pandas_Fillna - Fatal编程技术网

Python 如何一次为多个列填充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

在上面的示例数据框中,我有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']].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()