来自 Pandas df 的绘图集和类型错误不可散列的类型:'set' [英] Plotting sets from Pandas df and Type Error unhashable type: 'set'

查看:60
本文介绍了来自 Pandas df 的绘图集和类型错误不可散列的类型:'set'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一列集合的数据框:

I have a dataframe with a column of sets:

df = pd.DataFrame([['Alex',{33, 34}],['Bob',{33}],['Clarke',{33, 34}]], columns = ['names', 'indicators']).

我想绘制 df['indicators'] 中值的频率,以了解发生了哪些指标组合.即,在这里我想要一个显示 {33, 34} 出现两次而 {33} 出现一次的情节.让 seaborn 成为某人,我通常会制作直方图:

I would like to plot the frequency of values in df['indicators'] to get a sense of what combinations of indicators take place. I.e., here I'd like a plot that shows {33, 34} shows up twice and {33} shows up once. Letting seaborn be sb, I'd normally make a histogram:

sb.countplot(data = df, x = 'indicators')

但是当我尝试这个时,我得到 TypeError: unhashable type: 'set' .我试过将参数从集合转换为列表或 np.array() 无济于事.

But I get TypeError: unhashable type: 'set' when I try this. I've tried converting the arguments from sets to lists or np.array() to no avail.

推荐答案

import seaborn as sns

通过astype()尝试:

sns.countplot(data = df.astype({'indicators':'str'}), x = 'indicators')
#the 'indicators' column in you real dataset won't change

df['indicators']=df['indicators'].astype(str)
#Finally:
sns.countplot(data = df, x = 'indicators')
#For making back the the 'indicators' column back to set you can use:
df['indicators']=df['indicators'].str.strip('{}').str.split(',').map(set)

输出:

这篇关于来自 Pandas df 的绘图集和类型错误不可散列的类型:'set'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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