1.重复值的处理方法:所有字段完全相同的重复值,一般直接删除,即只保留一个。
2.缺失值的处理方法:
缺失值占比很高,可以删除(超过60%)
缺失值占比不高:使用均值,中位数,众数填充
缺失值占比不高:用模型拟合填充
3.使用零售电商数据进行举例:
代码:
import pandas as pd
# 各个字段的含义
#
# InvoiceNo:发票编码,一般为 6 位整数,C 开头表示退货订单
# StockCode:产品代码,一般为 5 位整数
# Description:商品名称
# Quantity:下单数量
# UnitPrice:每单位商品价格,以英镑表示
# InvoiceDate:下单时间
# CustomerID:顾客ID,一般为 5 位整数
# Country:顾客所在国家或地区
# 这个csv文件的编码格式是ISO-8859-1
online_retail_pd = pd.read_csv(r‘D:\pycharm_project\电商背后的数据密码\第二章 数据分析\online_retail.csv‘, encoding=‘ISO-8859-1‘)
# print(online_retail_pd.info())
# print(online_retail_pd.head())
# 所有的字段值都是一样的话,那么这类数据没有参考意义
# 显示出重复的值,keep=False保留所有的重复行
duplicated_data_index = online_retail_pd.duplicated(keep=False)
# 排序,排序的好处就是我能够很清楚的了解到每一个重复的值
online_retail_pd[duplicated_data_index].sort_values(
[‘InvoiceNo‘, ‘StockCode‘, ‘Description‘, ‘Quantity‘, ‘InvoiceDate‘, ‘UnitPrice‘, ‘Country‘, ‘CustomerID‘])
print(‘删除重复值之前有‘+str(len(online_retail_pd))+‘行‘)
# inplace=True,删除重复值,重复的值只保留一行 删除前是541909行数据
online_retail_pd.drop_duplicates(inplace=True)
# 删除后是536641行数据
print(‘删除重复值之后有‘+str(len(online_retail_pd))+‘行‘)
# print(online_retail_pd.info())
# <class ‘pandas.core.frame.DataFrame‘>
# Int64Index: 536641 entries, 0 to 541908
# Data columns (total 8 columns):
# # Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 InvoiceNo 536641 non-null object
# 1 StockCode 536641 non-null object
# 2 Description 535187 non-null object
# 3 Quantity 536641 non-null int64
# 4 InvoiceDate 536641 non-null object
# 5 UnitPrice 536641 non-null float64
# 6 CustomerID 401604 non-null float64
# 7 Country 536641 non-null object
# dtypes: float64(2), int64(1), object(5)
# memory usage: 36.8+ MB
# None
# 通过观察可以发现,Description是有缺失值的,但是缺失值处理,
# 商品描述缺失的话,对我们的影响不大,但是顾客id(CustomerID)是比较重要的,顾客id缺失的话,我们是不能用均值,中位数等去填充的,一旦填充的话,会严重影响结果。此时只能把这些缺失顾客id的行全部删掉
# 删除CustomerID等于空的
online_retail_pd.dropna(subset=[‘CustomerID‘], inplace=True)
print(online_retail_pd.info())
删除重复值之前有541909行
删除重复值之后有536641行
before:
# <class ‘pandas.core.frame.DataFrame‘>
# Int64Index: 536641 entries, 0 to 541908
# Data columns (total 8 columns):
# # Column Non-Null Count Dtype
# --- ------ -------------- -----
# 0 InvoiceNo 536641 non-null object
# 1 StockCode 536641 non-null object
# 2 Description 535187 non-null object
# 3 Quantity 536641 non-null int64
# 4 InvoiceDate 536641 non-null object
# 5 UnitPrice 536641 non-null float64
# 6 CustomerID 401604 non-null float64
# 7 Country 536641 non-null object
# dtypes: float64(2), int64(1), object(5)
# memory usage: 36.8+ MB
# None
after:
<class ‘pandas.core.frame.DataFrame‘>
Int64Index: 401604 entries, 0 to 541908
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 InvoiceNo 401604 non-null object
1 StockCode 401604 non-null object
2 Description 401604 non-null object
3 Quantity 401604 non-null int64
4 InvoiceDate 401604 non-null object
5 UnitPrice 401604 non-null float64
6 CustomerID 401604 non-null float64
7 Country 401604 non-null object
dtypes: float64(2), int64(1), object(5)
memory usage: 27.6+ MB
None