首页 > 解决方案 > 为条形图图例熊猫中的整数分配标签

问题描述

我想将“单身”分配给 0,将“已婚”分配给 1,并将其显示在条形图中 在此处输入图像描述

这是我写的代码:

ds["Marital_Status"].value_counts().plot(kind='bar',color=['green', 'cyan'], rot=0)

plt.title('买家婚姻状况')

标签: pandasbar-chart

解决方案


使用Series.map

(  ds["Marital_Status"].map({0:'Single',1:'Married'})
                       .value_counts()
                       .plot(kind='bar',color=['green', 'cyan'], rot=0) )
   # if is string type
   #ds["Marital_Status"].map({'0':'Single','1':'Married'}) 

推荐阅读