gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:11:53 25 4
gpt4 key购买 nike

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

# 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")

我得到的错误信息是 “类型错误:输入类型不支持 ufunc 'isnan',并且无法根据转换规则 ''safe'' 将输入安全地强制转换为任何受支持的类型” .

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

最佳答案

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

sns.heatmap(data_uni, cmap="YlGnBu", mask=data_uni.isnull())

关于python - 来自带有 NaN 的 Pandas 数据框的 seaborn 热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53799537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com