如何从Pivot Table()结果在python中绘制条形图?

2024-05-14 20:53:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我根据员工的“性别”和“教育背景”计算了公司的员工总数。现在我想以条形图的形式将其可视化,但我已经尝试过了,但仍然无法直观地查看它

代码:

df1 = pd.pivot_table(comp2, index = ['Gender', 'EducationBackground'], aggfunc={'EducationBackground':'count'})

结果:

"""
                             EducationBackground
Gender  EducationBackground 
Female        Life Sciences                   30
                  Marketing                   15
                    Medical                   21
                      Other                    2
           Technical Degree                    7
  Male      Human Resources                    3
              Life Sciences                   48
                  Marketing                   14
                    Medical                   42
                      Other                    1
           Technical Degree                   11

想象:

sns.countplot(df1,hue='EducationBackground')

错误:

Could not interpret input 'EducationBackground'

Tags: 员工公司gendermarketingmedicaldf1背景other
1条回答
网友
1楼 · 发布于 2024-05-14 20:53:48

sns.countplot已经执行了聚合(类似于您在pivot_table中所做的操作),因此您可能可以通过直接在comp2数据帧上使用sns.countplot来获得所需的内容,例如:

sns.countplot(x='EducationBackground', hue="Gender", data=comp2)

注意:在df1上使用sns.barplot可以获得类似的结果

相关问题 更多 >

    热门问题