数据基本探索(python数据分析与挖掘实战篇)

# -*- coding: utf-8 -*-
"""
Created on Mon Jul  2 09:33:58 2018

@author: 87671
"""

###############
#数据探索
###############

import pandas as pd
data=pd.read_excel('catering_sale.xls',index_col=u'日期')#指定日期为index 
data.describe()

########检测异常值
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

plt.figure()
p=data.boxplot(return_type='dict')
# 'flies'即为异常值的标签.[0]是用来标注第1列的异常值数值,同理[i]标注第i+1列的异常值.
x=p['fliers'][0].get_xdata()#数据只有一个列所以【0】
y=p['fliers'][0].get_ydata()
y.sort()
#用annotate添加注释 
for i in range(len(x)):
    if i>0:
        plt.annotate(y[i],xy=(x[i],y[i]),xytext=(x[i]+0.05-0.8/(y[i]-y[i-1]),y[i]))
    else:
        plt.annotate(y[i],xy=(x[i],y[i]),xytext=(x[i]+0.08,y[i]))       
plt.show()

#########数据特征分析 
##定量数据分析
##定性数据分析 

data=data[(data[u'销量']>400) & (data[u'销量']<5000)]#过滤异常数据 
statistics=data.describe()

statistics.loc['range']=statistics.loc['max']-statistics.loc['min']#极差
statistics.loc['var']=statistics.loc['std']/statistics.loc['mean']#变异系数
statistics.loc['dis']=statistics.loc['75%']-statistics.loc['25%']#四分位数间距 

print(statistics)


##########周期性分析 (累加图)
from __future__ import print_function
data2=pd.read_excel('catering_dish_profit.xls',index=u'菜品名')
data2_copy=data2['盈利'].copy()
'''
AttributeError:'DataFrame' object has no attribute 'sort'
解决:将“sort”改为“sort_values”
'''
data2_copy.sort_values(ascending=False)

plt.figure()
data2_copy.plot(kind='bar')
plt.ylabel('盈利(元)')
#图上的第二条线
p2=1.0*data2_copy.cumsum()/data.sum()#查看一下p2[6]是85%
p2.plot(color='r',secondary_y=True,style='-o',linewidth=2)
#显示的数格式 保留4位小数 
plt.annotate(format(p2[6],'.4%'), xy = (6, p2[6]), xytext=(6*0.9, p2[6]*0.9), arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")) #添加注释,即85%处的标记。这里包括了指定箭头样式。
plt.ylabel('盈利(比例)')
plt.show()


############相关性分析 
data3=pd.read_excel('catering_sale_all.xls',index=u'日期')
data3.corr()#相关系数矩阵 
data3.corr()[u'百合酱蒸凤爪‘]#只显示百合酱凤爪的和其他才是的相关系数 
data3[u'百合酱蒸凤爪'].corr(data3[u'翡翠蒸香茜饺']) #计算“百合酱蒸凤爪”与“翡翠蒸香茜饺”的相关系数
'''
s1=df.loc[0]#提取第一行
s2=df.loc[1]#提取第二行 
s1.corr(s2,method='pearson'/'spearman')
'''


猜你喜欢

转载自blog.csdn.net/kylin_learn/article/details/80882101