Python 在4个单独的绘图上,每个评级的绘图组平均值
我有4个小组(研究、销售、制造、人力资源),每个小组有2个类别(0和1)。我试图在列表中的功能中绘制每组的平均分数Python 在4个单独的绘图上,每个评级的绘图组平均值,python,pandas,matplotlib,pandas-groupby,seaborn,Python,Pandas,Matplotlib,Pandas Groupby,Seaborn,我有4个小组(研究、销售、制造、人力资源),每个小组有2个类别(0和1)。我试图在列表中的功能中绘制每组的平均分数评分。给我提供方法的代码如下所示(使用depts=['research'、'sales'、'manu'、'hr']: ratings = ['JobSatisfaction', 'PerformanceRating', 'EnvironmentSatisfaction', 'RelationshipSatisfaction'] for i in depts: for x
评分
。给我提供方法的代码如下所示(使用depts=['research'、'sales'、'manu'、'hr']
:
ratings = ['JobSatisfaction', 'PerformanceRating', 'EnvironmentSatisfaction', 'RelationshipSatisfaction']
for i in depts:
for x in ratings:
print(group_data.groupby([i]).mean()[x])
这将导致此输出:
research
0.0 2.700000
1.0 2.773973
Name: JobSatisfaction, dtype: float64
research
0.0 3.100000
1.0 3.167808
Name: PerformanceRating, dtype: float64
research
0.0 2.500000
1.0 2.726027
Name: EnvironmentSatisfaction, dtype: float64
research
0.0 2.687500
1.0 2.705479
Name: RelationshipSatisfaction, dtype: float64
sales
0.0 2.754601
1.0 2.734940
Name: JobSatisfaction, dtype: float64
sales
0.0 3.125767
1.0 3.144578
Name: PerformanceRating, dtype: float64
sales
0.0 2.671779
1.0 2.734940
Name: EnvironmentSatisfaction, dtype: float64
sales
0.0 2.702454
1.0 2.602410
Name: RelationshipSatisfaction, dtype: float64
manu
0.0 2.682759
1.0 2.723077
Name: JobSatisfaction, dtype: float64
manu
0.0 3.186207
1.0 3.158974
Name: PerformanceRating, dtype: float64
manu
0.0 2.917241
1.0 2.735897
Name: EnvironmentSatisfaction, dtype: float64
manu
0.0 2.724138
1.0 2.689744
Name: RelationshipSatisfaction, dtype: float64
hr
0.0 2.705882
1.0 2.557692
Name: JobSatisfaction, dtype: float64
hr
0.0 3.196078
1.0 3.134615
Name: PerformanceRating, dtype: float64
hr
0.0 2.764706
1.0 2.596154
Name: EnvironmentSatisfaction, dtype: float64
hr
0.0 2.813725
1.0 2.961538
Name: RelationshipSatisfaction, dtype: float64
我的问题是,我如何在4个不同的条形图上绘制每个评级的组平均值(研究、销售、制造、人力资源)[‘工作满意度’、‘绩效经营’、‘环境满意度’、‘关系满意度’],以便我能直观地看到和比较各组之间的差异
我的数据来自IBM HR数据集:您可以使用seaborn提供的
sns.barplot
,因为您的y变量是可比较的,所以可以按颜色和相同的y轴分隔:
import statsmodels.api as sm
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv("WA_Fn-UseC_-HR-Employee-Attrition.csv")
ratings = ['JobSatisfaction', 'PerformanceRating', 'EnvironmentSatisfaction', 'RelationshipSatisfaction']
sns.barplot(data = df[['Department'] + ratings].melt(id_vars='Department'),
x = 'variable',y='value',hue='Department')
plt.xticks(rotation=45)