1电商平台零售数据分析
环境为Anaconda3,Python3.8,用JupyterNotebook写的
数据文件在百度网盘
链接:https://pan.baidu.com/s/1DwWF4Vpduj8SqRUUezt4ew
提取码:2222
import plotly as py
py.offline.init_notebook_mode()
pyplot = py.offline.iplot
import plotly.graph_objs as go
from plotly.graph_objs import Scatter
from scipy import stats
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import os
os.chdir(r'E:\2020年Python数据分析师特训营全套84节视频完结版(就业向零基础友好)\2020年Python数据分析师特训营全套84节视频完结版')
df = pd.read_excel('超市数据.xlsx', dtype= {
'Row ID':str})
df
#缺失率
df.apply(lambda x:sum(x.isnull())/len(x),axis=0)
#拷贝原文件为df1,防止数据丢失
df1 = df.copy()
df1
#去掉有缺失值的数据
df1.dropna(how='any')
# #去掉指定的两列
# df1.drop(['Unnamed: 24','Unnamed: 25'],axis=1,inplace=True)
#将Shipping Cost这一列中的缺失值替换为U
df['Shipping Cost'] = df['Shipping Cost'].fillna('U')
#先导入re模块,可以使用split一次加入多个切割条件
import re
#取出Ship Date这一列的每行字符串中以/分隔开的第一、二、三个元素
df1['ship_day'] = [re.split('\/|\-',x)[0] for x in df1['Ship Date']]
df1['ship_month'] = [re.split('\/|\-',x)[1] for x in df1['Ship Date']]
df1['ship_year'] = [re.split('\/|\-',x)[2] for x in df1['Ship Date']]
#将Order Date转换为datetime格式
df1['Order Date'] = pd.to_datetime(df1['Order Date'],errors='coerce')
df1['Order Date'] = df1['Order Date'].dt.date
#去重
df1 = df1.drop_duplicates()
#查看数据类型
df1.info()
#异常值处理之前先探索数据
df1.describe()
#将Sales Quantity Profit转换为float64
df1['Sales'] = pd.to_numeric(df1['Sales'], errors='coerce')
df1['Quantity'] = pd.to_numeric(df1['Quantity'], errors='coerce')
df1['Profit'] = pd.to_numeric(df1['Profit'], errors='coerce')
#计算Sales*Quantity保存为新的一列Price
df1['Price'] = df1.apply(lambda x:x['Sales']*x['Quantity'],axis