数据框:
TARGET NAME_INCOME_TYPE count percent_rate
0 NO Working 46924 90.408847
1 NO Commercial associate 21492 41.408809
2 NO Pensioner 16879 32.520905
3 NO State servant 6593 12.702786
4 NO Student 9 0.017340
5 NO Unemployed 6 0.011560
6 NO Businessman 4 0.007707
7 NO Maternity leave 1 0.001927
8 YES Working 4978 9.591153
9 YES Commercial associate 1726 3.325498
10 YES Pensioner 978 1.884320
11 YES State servant 407 0.784170
12 YES Unemployed 4 0.007707
我已经使用以下代码绘制了它:
def rename_target(status):
if status == 1:
return 'YES'
else:
return 'NO'
def grouper(train,var):
qq = train.groupby('TARGET')[var].value_counts().sort_values(ascending=True).reset_index(name='count')
totals = list()
for c in qq[var].values:
totals.append(np.sum(qq[qq[var] == c]['count']))
qq['totals'] = pd.Series(totals,index=qq.index)
qq['percent_rate'] = (qq['count']*100) / qq['totals']
fig,ax1 = plt.subplots(figsize=(25,10))
qq['TARGET'] = qq['TARGET'].apply(lambda status: rename_target(status))
sns.barplot(x=var,y='percent_rate',hue='TARGET',data=qq,palette='rainbow',saturation=0.5,hue_order=['YES','NO'],ax=ax1,)
plt.xlabel(s=var,size=20)
plt.ylabel(s= '%',size=20)
plt.xticks(rotation=90,size=20)
plt.yticks(size=20)
plt.title(var + " vs " + " % count",size=20)
plt.legend(loc='right',prop={'size':20})
plt.show()
grouper(train,'NAME_INCOME_TYPE') #this calls the function and plots
图表具有按目标分组的条形,即hue ='target'。
但它们的顺序不正确,我需要按“目标”来设置reset_index,但这是绝对变量。
我如何按顺序绘制条形图?
sns.barplot(x=var,order = qq['TARGET']) # not working