如何从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它很管用,真的帮了我很多。如果你的答案确实解决了你的问题,请考虑接受答案。