Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Pivot Table()结果在python中绘制条形图?_Python_Matplotlib_Data Science_Seaborn_Data Visualization - Fatal编程技术网

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

如何从Pivot Table()结果在python中绘制条形图?,python,matplotlib,data-science,seaborn,data-visualization,Python,Matplotlib,Data Science,Seaborn,Data Visualization,我根据员工的“性别”和“教育背景”计算了公司的员工总数。现在我想以条形图的形式将其可视化,但我已经尝试过了,但仍然无法直观地查看它 代码: df1 = pd.pivot_table(comp2, index = ['Gender', 'EducationBackground'], aggfunc={'EducationBackground':'count'}) 结果: """ EducationBackgr

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

代码:

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'

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

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

注意:您可以在
df1
sns上使用
sns.barplot
获得类似的结果。countplot
已经执行了聚合(类似于您在
pivot\u表中所做的操作),因此您可能可以直接在
comp2
数据框上使用
sns.countplot
来获得所需,比如:

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

注意:在
df1

上使用
sns.barplot
可以获得类似的结果。请尝试
hue=df1.EducationBackground
您可能会遇到错误,因为索引名和列名相同,那么为什么不更改列名呢?请尝试
hue=df1.EducationBackground
您可能会遇到错误,因为索引名和列名是相同的,那么为什么不更改列名呢?是的@rjg它很管用,确实帮了我很多忙。如果你的答案确实解决了你的问题,请考虑接受答案。是的@rjg它很管用,真的帮了我很多。如果你的答案确实解决了你的问题,请考虑接受答案。