簡(jiǎn)書(shū)展示jupyter notebook的分布輸出結(jié)果有問(wèn)題,需要看分步結(jié)果的可以到我的CSND博客
https://blog.csdn.net/Itachi_dream
#!/usr/bin/env python
# coding: utf-8
# 明確分析目的:了解2016年交易數(shù)據(jù)中的商品表現(xiàn)、城市分布、渠道信息、猶豫時(shí)間等
# 訂單總數(shù)、銷售總額、總下單客戶數(shù)
# 銷量在前十名和后十名的商品
# 成交額在前十名和后十名的商品
# 銷量和銷售額最后100個(gè)的交集
# 成交額的區(qū)間分布;# 細(xì)分價(jià)格區(qū)間的成交額分布
# 不同城市的成交額、銷量 取前十名降序排列(成交額、銷量)
# 成交額占比最前三名的渠道及其所在城市
# 加載數(shù)據(jù)分析需要使?的庫(kù)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 加載數(shù)據(jù),加載之前先??本編輯器看下數(shù)據(jù)的格式,??是什么,分隔符是什么等
df = pd.read_csv('./order_info_2016.csv', index_col = 'id')
df.info()
df.head()
# 數(shù)值型數(shù)據(jù)統(tǒng)計(jì)
df.describe()
# 加載device_type
device_type = pd.read_csv('./device_type.txt')
device_type
# 數(shù)據(jù)清洗
# orderId
df.orderId.unique().size# 查看orderId 訂單號(hào)是否有重復(fù)值
df.orderId.unique().size < df.orderId.size # 返回值為T(mén)RUE,說(shuō)明有重復(fù)值
# 有重復(fù)值?般最后處理,因?yàn)槠渌牧锌赡軙?huì)影響到刪除哪?條重復(fù)的記錄
# userId
# 在df.info()中可見(jiàn) userId 104557 non-null int64,數(shù)量和Index總數(shù)相同
# 在df.describe()展示的數(shù)據(jù)也沒(méi)有什么異常
# productId
# 在df.info()中可見(jiàn) productId 104557 non-null int64,數(shù)量和Index總數(shù)相同
# 在df.describe()看到productId最小值為0
df.productId[df['productId'] == 0].size # 查看productId為0的條目數(shù)
# df[df['productId'] == 0].size # 注意不能這樣使用,因?yàn)閐f是dataframe,它的size值是行*列的數(shù)目
# productId為0的條目數(shù)較少,等全部數(shù)據(jù)處理完后刪除
# cityId
# 在df.info()中可見(jiàn) cityId 104557 non-null int64,數(shù)量和Index總數(shù)相同
# describe中也沒(méi)有異常
df.cityId.unique().size #城市數(shù)目
# price
# 同上,無(wú)異常
df['price'] = df['price']/100 # 轉(zhuǎn)換單位 分-->元
# payMoney
# info :payMoney 104557 non-null int64 無(wú)空值
# 注意到describe中payMoney最小值為負(fù)值,刪除負(fù)值記錄
df.drop(df[df['payMoney'] < 0].index, inplace = True)
df[df.payMoney < 0].index # 顯示已刪除
df['payMoney'] = df['payMoney']/100 # 變成元
# channelId
# channelId根據(jù)info的結(jié)果,有些null的數(shù)據(jù),可能是端的bug等原因,在下單的時(shí)候沒(méi)有傳channelId字段
# 由于異常值較少,選擇刪除
df.drop(df[df['channelId'].isnull()].index, inplace = True)
# df[df['channelId'].isnull()] # 結(jié)果可見(jiàn)已刪除
# deviceType
# deviceType的取值可以看device_type.txt?件,沒(méi)有問(wèn)題,不需要處理
# createTime 和 payTime
# 由df.info() 可知二者沒(méi)有null值
# 分析目的要求分析2016年數(shù)據(jù),以createTime為準(zhǔn)對(duì)年份進(jìn)行篩選
df['createTime'] = pd.to_datetime(df['createTime']) # 轉(zhuǎn)換成為datetime格式
df['payTime'] = pd.to_datetime(df['payTime'])
# print(df.dtypes) # 顯示已轉(zhuǎn)換成功
import datetime
# 創(chuàng)建起止時(shí)間
startTime = datetime.datetime(2016, 1, 1)
endTime = datetime.datetime(2016, 12, 12, 23, 59, 59)
# 刪除createTime在2016年以外的數(shù)據(jù)
df.drop(df[df['createTime'] < startTime].index, inplace = True)
df.drop(df[df['createTime'] > endTime].index, inplace = True)
# 刪除createTime在payTime之后的異常數(shù)據(jù)
df.drop(df[df['createTime'] > df['payTime']].index, inplace = True)
# 刪除 orderId 中的重復(fù)值
df.drop(df[df['orderId'].duplicated()].index, inplace = True)
print(df[df['orderId'].duplicated()]) # 空集則刪除成功
# 刪除productId為0的數(shù)據(jù)
df.drop(df[df['productId'] == 0].index, inplace = True)
# 數(shù)據(jù)清洗完畢,進(jìn)行分析
# 數(shù)據(jù)總體情況
print('總訂單量',df.orderId.count())
print('總用戶量',df.userId.count())
print('總銷售額(元)',df.payMoney.sum())
# print('有流水的商品數(shù)',df.productId.unique().count()) #AttributeError: 'numpy.ndarray' object has no attribute 'count'
print('有流水的商品數(shù)',df.productId.unique().size)
# 分析數(shù)據(jù)可以從兩??開(kāi)始考慮,?個(gè)是維度,?個(gè)是指標(biāo),維度可以看做x軸,指標(biāo)可以看成是y軸,同?個(gè)維度可以分析多個(gè)指標(biāo),同?個(gè)維度也可以做降維升維。
# 銷量在前十名和后十名的商品
df_pro = df.groupby('productId', as_index = False)
df_pro_sort = df_pro.count()[['productId', 'orderId']]
df_pro_sort.rename(columns = {'orderId':'sale_sum'}, inplace = True)
df_pro_sort = df_pro_sort.sort_values('sale_sum')
print('銷量前十名商品','\n',df_pro_sort.head())
print('銷量后十名商品','\n',df_pro_sort.tail())
# 銷量和銷售額最后100個(gè)的交集
df_pro_totalprice = df_pro.sum()[['productId', 'payMoney']] # 商品銷售額表
df_pro_totalprice.sort_values('payMoney', ascending = False, inplace = True)
df_weak_productID = df_pro_sort.tail(200).index.intersection(df_pro_totalprice.tail(200).index)
df_weak_productID # 沒(méi)有需要下架的商品ID
# 成交額的區(qū)間分布
# df.describe() payMoney從0到22930
#分桶
bins = np.arange(0, 24000, 2000)
bins_data = pd.cut(df['payMoney'], bins)
bin_counts = df['payMoney'].groupby(bins_data).count()
bin_counts.plot(kind='bar') # 可見(jiàn)絕大部分集中在0~2000區(qū)間段,應(yīng)對(duì)0~2000區(qū)間段細(xì)分
plt.show()
# 細(xì)分價(jià)格區(qū)間的成交額分布
df_xf = df[df['payMoney'] < 2000]
bins2 = np.arange(0, 2000, 200)
bins_data2 = pd.cut(df_xf['payMoney'], bins2)
bin_counts2 = df_xf['payMoney'].groupby(bins_data2).count()
bin_counts2.plot(kind='bar')
plt.show()
# 可見(jiàn)集中于0~800區(qū)間
# 不同城市的成交額、銷量 取前十名降序排列(成交額、銷量)
df_city = df.groupby('cityId', as_index = False)
df_city_sum = df_city.sum()[['cityId', 'payMoney']]
df_city_cou = df_city.count()[['cityId', 'orderId']]
df_city_sum_cou = pd.merge(df_city_sum, df_city_cou, on = 'cityId',how = 'inner')
df_city_sum_cou.sort_values(['payMoney', 'orderId'], ascending = False, inplace = True)
df_city_sum_cou.head(10)
# 成交額占比最前三名的渠道及其所在城市
df_city_channel = df.groupby(['cityId', 'channelId'], as_index = False)
df_city_channel_sum = df_city_channel.sum()[['channelId', 'payMoney', 'cityId']]
df_city_channel_sum.sort_values('payMoney', ascending = False, inplace = True)
df_city_channel_sum.head(3)