数据探索与清洗

#数据质量分析
#数据特征分析(分布、对比、周期性、相关性、常见统计量)
#数据清洗
‘’’
1.缺失值处理(describe和len直接发现,通过0数据发现,比如淘宝商品价格为0)
处理方式:删除、插补、不处理
均值插补、中位数插补、众数插补、固定值插补(如固定的苹果价格)、最近数据插补、
回归插补、拉格朗日插值、牛顿插值、分段插值。
2.异常值处理(通过散点图发现)
处理方式:视为缺失值、删除、修补(平均数、中位数)、不处理
‘’’
import pymysql
import numpy as np
import pandas as pd
import matplotlib.pylab as pyl
‘’’
conn = pymysql.connect(host=‘127.0.0.1’,user=‘root’,passwd=‘root’,db=‘dd’)
sql = “select * from dangdang”
data = pd.read_sql(sql,conn)
print(data.describe())
‘’’
data = pd.read_excel(‘F:/result/dangdang.xls’)
print(data.describe())
print(len(data))

数据清洗,发现缺失值

将price=0的数据改为中位数

‘’’
x = 0
data[‘price’][(data[‘price’]==0)]=None
for i in data.columns:
for j in range(len(data)):
if(data[i].isnull())[j]:
data[i][j] = ‘48.9’
x+=1
print(x)
‘’’

异常值处理

#画散点图(横轴价格,纵轴为评论数)
#得到价格
data1 = data.T
price = data1.values[3]
#得到评论数据
comt = data1.values[2]
pyl.plot(price,comt,‘o’)
pyl.show()

comt异常>18000,price异常>600

line = len(data.values)
lie = len(data.values[0])
da = data.values
for i in range(0,line):
for j in range(0,lie):
if(da[i][3]>600):
print(da[i][j])
da[i][j]=48.9
if (da[i][2]>18000):
print(da[i][j])
da[i][j]= 321
da2 = da.T
price = da2[3]
comt = da2[2]
pyl.plot(price, comt, ‘oy’)
pyl.show()

分布情况

极差(max-min) 组距:极差/组数(12好看)

pricemax = da2[3].max()
pricemin = da2[3].min()
comtmax = da2[2].max()
comtmin = da2[2].min()
#极差
pricerg = pricemax - pricemin
comtrg = comtmax - comtmin

#组距
pricedst = pricerg/12
comtdst = comtrg/12
‘’’
#画价格直方图
pricesty = np.arange(pricemin,pricemax,pricedst)
pyl.hist(da2[3],pricesty)
pyl.show()

评论直方图

comtsty = np.arange(comtmin,comtmax,comtdst)
pyl.hist(da2[2],comtsty)
pyl.show()
‘’’

猜你喜欢

转载自blog.csdn.net/qq_43487620/article/details/88207106