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.
我有字典,用cars = pd.DataFrame.from_dict(cars_dict, orient='index')创建了熊猫,并对索引进行排序(按字母顺序排列的cars中的列= cars.sort_index(axis=1))。nan值吗?print(cars.isnull().any())和所有列都显示为false。
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..
我尝试了不同的方法来将这些“NaN”值转换为零,这是我想要做的,但是没有工作。我尝试过替换和fillna方法,以下都是我的dataframe示例。
speedtest size
toyota 65 NaN
honda 77 800
2
Either use replace
or np.where
on the values if they are strings:
使用replace或np。如果值是字符串,则在其中:
df = df.replace('NaN', 0)
Or,
或者,
df[:] = np.where(df.eq('NaN'), 0, df)
Or, if they're actually NaNs (which, it seems is unlikely), then use fillna
:
或者,如果它们实际上是无元数(这似乎不太可能),那么使用fillna:
df.fillna(0, inplace=True)
Or, to handle both situations at the same time, use apply
+ pd.to_numeric
(slightly slower but guaranteed to work in any case):
或者,要同时处理这两种情况,使用apply + pd。to_numeric(稍微慢一点但保证在任何情况下工作):
df = df.apply(to_numeric, errors='coerce').fillna(0, downcast='infer')
Thanks to piRSquared for this one!
多亏了piRSquared给这个!
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2018/02/23/3a9cd1dbcac660522c57e5274ce79b7a.html。