df.fillna(0)命令不会将NaN值替换为0 [英] df.fillna(0) command won't replace NaN values with 0

查看:7451
本文介绍了df.fillna(0)命令不会将NaN值替换为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将下面的代码中生成的NaN值替换为0.我不明白下面的内容将无法正常工作。它仍然保持NaN值。

I'm trying to replace the NaN values generated in the code below to 0. I don't understand what the below won't work. It still keeps the NaN values.

df_pubs=pd.read_sql("select Conference, Year, count(*) as totalPubs from publications where year>=1991 group by conference, year", db)

df_pubs['Conference'] = df_pubs['Conference'].str.encode('utf-8')

df_pubs = df_pubs.pivot(index='Conference', columns='Year', values='totalPubs')
df_pubs.fillna(0)

print df_pubs

print df produce this:

Year                                                                                       1991  \
Conference                                                                                        
                                                                                            223   
10th Anniversary Colloquium of UNU/IIST                                                     NaN   
15. WLP                                                                                     NaN   
1999 ACM SIGMOD Workshop on Research Issues in Data Mining and Knowledge Discovery          NaN   
25 Years CSP                                                                                NaN  


推荐答案

您需要分配 fillna 的结果:

df_pubs = df_pubs.fillna(0)

通过param inplace = True

df_pubs.fillna(0, inplace=True)

请参阅 docs

您可以修改你的代码为:

You could modify your code to this:

df_pubs = df_pubs.pivot(index='Conference', columns='Year', values='totalPubs').fillna(0)

哪些工作,但它是有争议的是, fillna 是否可读。

which would work but it's debatable whether the fillna is readable here.

这篇关于df.fillna(0)命令不会将NaN值替换为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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