1、导入包
import numpy as np
import pandas as pd
import re
from bs4 import BeautifulSoup
from IPython import display
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
%matplotlib inline
2、导入数据
df = pd.read_csv('/root/haiyan/new_deeplearning/04_1.csv')
3、统计消费性别倾向
df_2 = pd.DataFrame(df['消费性别倾向'].value_counts())
df_2.plot(kind='bar')
plt.show()
4、条形图上显示每个类别的占比
#将Series转为DataFrame结构
df_1 = pd.DataFrame(df['消费性别倾向'].value_counts())
#将统计数列命名为'count'
df_1.columns = ['count']
#添加百分比列
df_1['per'] = df_1['count']/(df_1['count'].sum()) *100
index = np.arange(len(df_1.index))
ticks = [unicode(i) for i in df_1.index]
#加载字体,因为统计类别中有中文
font= FontProperties(fname='/usr/share/fonts/wqy-microhei/wqy-microhei.ttc', size = 10)
plt.bar(index, df_1['count'])
plt.xticks(index, ticks, ha ='left',rotation=0, fontproperties = font)
plt.title(u'消费性别倾向', fontproperties = font)
效果图,如下
5、遗留问题
统计结果中存在汉字,label显示不完全,还在查找解决办法,有知道的朋友可以留言相告,感激不尽