import pandas as pd
import numpy as np
order = pd.read_table('meal_order_info.csv',
sep=',',encoding='gbk')
print('原始数据:\n',order.shape)
# 由于表中的日期数据为字符串类型,
# 所以在进行日期操作之前,
# 需要将它们转成TimeStape
order['use_start_time'] = pd.to_datetime(order['use_start_time'])
order['lock_time'] = pd.to_datetime(order['lock_time'])
# 时间差
order['time'] = order['lock_time']-order['use_start_time']
print(order['time'].head())
# 异常值的处理
# 1.判断这一列是否有空值,如果有删除该行数据
index_null = order.index[order['time'].isnull()]
# 删除 inplace是否作用在原始数据表
order.drop(labels=index_null,axis=0,inplace=True)
print('剔除空值后表的数据:\n',order.shape)
# 2.关于负值的清洗,lock_time<start_time
mask = order['lock_time']<order['use_start_time']
index_positive = order.index[mask]
order.drop(labels=index_positive,axis=0,inplace=True)
print('剔除time为负值后的数据:\n',order.shape)
print(order['time'].head())
# 点餐时间大于1天为异常数据
# print(order['time'].tail(50))
# fp = pd.ExcelWriter('清洗数据.xlsx')
order.to_excel('清洗数据.xlsx','Sheet1')
# fp.save()
# str
# 序列.str:表示将序列中的元素(按行),以字符串的形式来展示
# 生成过滤序列[False,True.........]把True过滤掉
mask = order['time'].astype('str').str.split().str[0].astype('int')>0#字符串中第一位的值
# print(order['time'].astype('str').str.split().str[0])
index_exception = order.index[mask]
order.drop(labels=index_exception,axis=0,inplace=True)
print('异常值清洗后的标的数据为:',order.shape)
print(order['time'].min())
print(order['time'].max())
print(order['time'].mean())
数据分析(pandas)---09.案例
最新推荐文章于 2020-11-19 15:07:57 发布