# -*- coding: utf-8 -*-
"""
Created on Mon Jun 24 11:00:09 2019
@author: User
"""
# 《Python数据分析基础》中国统计出版社
import numpy as np
from scipy import stats
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
myfont=FontProperties(fname='data\msyh.ttc')
dc_sales = pd.read_csv(u'data\\ch7\\dc_sales.csv',encoding = "gbk")
print(dc_sales.head())
dc_sales['pixel'] = dc_sales['pixel'].astype('category')
dc_sales['pixel'].cat.categories=['500万像素及以下','500-600万像素',
'600-800万像素','800-1000万像素',
'1000万像素及以上']
#dc_sales['pixel'].cat.set_categories=['500万像素及以下','500-600万像素',
# '600-800万像素','800-1000万像素',
# '1000万像素及以上']
print(dc_sales.head())
print(pd.pivot_table(dc_sales,
index=['pixel'],
columns=['market'],
values=['sales'],
aggfunc='sum'))
G=dc_sales['pixel'].unique() #G用于统计变量pixel的像素属性
args=[] #列表args用于存储不同像素属性下的销售数据
for i in list(G):
args.append(dc_sales[dc_sales['pixel']==i]['sales'])
#绘制盒须图
dc_sales_plot=plt.boxplot(args,vert=True,patch_artist=True)
#colors=['',]
fig=plt.gcf()
fig.set_size_inches(8,5)
combinebox=plt.subplot(111)
combinebox.set_xticklabels(G, fontproperties=myfont)
plt.show()
运行:
market pixel sales
0 1 1 70
1 1 2 101
2 1 3 114
3 1 4 120
4 1 5 132
market pixel sales
0 1 500万像素及以下 70
1 1 500-600万像素 101
2 1 600-800万像素 114
3 1 800-1000万像素 120
4 1 1000万像素及以上 132
sales
market 1 2 3 4 5 6 7 8
pixel
500万像素及以下 70 67 82 87 80 80 87 96
500-600万像素 101 76 97 88 92 99 123 90
600-800万像素 114 96 128 103 107 91 99 119
800-1000万像素 120 98 132 128 132 132 131 119
1000万像素及以上 132 102 123 119 123 135 126 117