将数据帧中的NaN转换为零 [英] Converting NaN in dataframe to zero

查看:298
本文介绍了将数据帧中的NaN转换为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字典并使用创建了熊猫 汽车= pd.DataFrame.from_dict(cars_dict,orient ='index') 和 对索引进行排序(列按字母顺序
汽车= cars.sort_index(axis = 1) 排序后,我发现DataFrame具有NaN,我不确定 如果真的是np.nan值? print(cars.isnull().any())并且所有列都显示为false.

I have dictionary and created Pandas using cars = pd.DataFrame.from_dict(cars_dict, orient='index') and sorted the index (columns in alphabetical order
cars = cars.sort_index(axis=1) After sorting I noticed the DataFrame has NaN and I wasn't sure if the really np.nan values? print(cars.isnull().any()) and all column shows false.

我尝试了不同的方法将那些"NaN"值转换为零,这是我想要做的,但是没有一个在工作. 我尝试了replace和fillna方法,但没有任何效果 以下是我的数据框示例.

I have tried different method to convert those "NaN" values to zero which is what I want to do but non of them is working. I have tried replace and fillna methods and nothing works Below is sample of my dataframe..

            speedtest          size 
toyota       65                NaN 
honda        77                800 

推荐答案

如果值是字符串,则对它们使用replacenp.where:

Either use replace or np.where on the values if they are strings:

df = df.replace('NaN', 0)

或者,

df[:] = np.where(df.eq('NaN'), 0, df)

或者,如果它们实际上是NaN(似乎不太可能),则使用fillna:

Or, if they're actually NaNs (which, it seems is unlikely), then use fillna:

df.fillna(0, inplace=True)

或者,要同时处理这两种情况,请使用apply + pd.to_numeric(速度稍慢,但可以保证在任何情况下都可以使用):

Or, to handle both situations at the same time, use apply + pd.to_numeric (slightly slower but guaranteed to work in any case):

df = df.apply(to_numeric, errors='coerce').fillna(0, downcast='infer')

感谢piRSquared!

Thanks to piRSquared for this one!

这篇关于将数据帧中的NaN转换为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆