来自带有 NaN 的 Pandas 数据框的 seaborn 热图

Jiw*_*Kim 4 python nan heatmap pandas seaborn

嗨,我真的很想创建一个热图,但我很挣扎:

# correlations between undergrad studies and occupation
data_uni = n.groupby(['Q5','Q6'])['Q6'].count().to_frame(name = 'count').reset_index()
# some participants did not answer the question in the survey
data_uni.fillna('Unknown', inplace=True)

data_uni.pivot(index='Q5', columns='Q6', values='count')
plt.figure(1, figsize=(14,10))
sns.heatmap(data_uni, cmap="YlGnBu")
Run Code Online (Sandbox Code Playgroud)

我得到的错误消息是"TypeError: ufunc 'isnan' not supported for the input types, and the input could not be safely coerced to any supported types 根据转换规则 ''safe''"

这是创建热图的正确方法吗?如果是,我做错了什么,如果不是,正确的方法是什么?感谢您的帮助!

cs9*_*s95 7

根据问题GH375,您可以指定一个掩码,其中不会显示掩码值为 的那些单元格的数据True

sns.heatmap(data_uni, cmap="YlGnBu", mask=data_uni.isnull())
Run Code Online (Sandbox Code Playgroud)