df.fillna(0) command won't replace NaN values with 0
我正在尝试将下面代码中生成的NaN值替换为0。我不明白下面的内容有什么用。它仍然保持NaN值。
1 2 3 4 5 6 7 8 | 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 |
1 2 3 4 5 6 7 | 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 |
您需要分配EDOCX1[0]的结果:
1 | df_pubs = df_pubs.fillna(0) |
或通过参数
1 | df_pubs.fillna(0, inplace=True) |
查看文档
您可以将代码修改为:
1 | df_pubs = df_pubs.pivot(index='Conference', columns='Year', values='totalPubs').fillna(0) |
这是可行的,但在这里是否可读