我正在尝试将以下代码中生成的NaN值替换为0。但我不明白为什么以下代码不起作用。它仍然保留着NaN值。
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
会输出以下内容:
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
df_pubs = df_pubs.fillna(0)
。fillna
不会修改原始的 DataFrame。 - TomAugspurgerinplace=True
。 - EdChum