数据清洗之 重复值处理

重复值处理

  • 数据清洗一般先从重复值和缺失值开始处理
  • 重复值一般采取删除法来处理
  • 但有些重复值不能删除,例如订单明细数据或交易明细数据等
import pandas as pd
import numpy as np
import os
os.getcwd()
'D:\\Jupyter\\notebook\\Python数据清洗实战\\数据清洗之数据预处理'
os.chdir('D:\\Jupyter\\notebook\\Python数据清洗实战\\数据')
df = pd.read_csv('MotorcycleData.csv', encoding='gbk', na_values='Na')
df.head(5)
ConditionCondition_DescPriceLocationModel_YearMileageExterior_ColorMakeWarrantyModel...Vehicle_TitleOBOFeedback_PercWatch_CountN_ReviewsSeller_StatusVehicle_TileAuctionBuy_NowBid_Count
0Usedmint!!! very low miles$11,412McHenry, Illinois, United States2013.016,000BlackHarley-DavidsonUnspecifiedTouring...NaNFALSE8.1NaN2427Private SellerClearTrueFALSE28.0
1UsedPerfect condition$17,200Fort Recovery, Ohio, United States2016.060BlackHarley-DavidsonVehicle has an existing warrantyTouring...NaNFALSE10017657Private SellerClearTrueTRUE0.0
2UsedNaN$3,872Chicago, Illinois, United States1970.025,763Silver/BlueBMWVehicle does NOT have an existing warrantyR-Series...NaNFALSE100NaN136NaNClearTrueFALSE26.0
3UsedCLEAN TITLE READY TO RIDE HOME$6,575Green Bay, Wisconsin, United States2009.033,142RedHarley-DavidsonNaNTouring...NaNFALSE100NaN2920DealerClearTrueFALSE11.0
4UsedNaN$10,000West Bend, Wisconsin, United States2012.017,800BlueHarley-DavidsonNO WARRANTYTouring...NaNFALSE10013271OWNERClearTrueTRUE0.0

5 rows × 22 columns

def f(x):
    if '$' in str(x):
        x = str(x).strip('$')
        x = str(x).replace(',', '')
    else:
        x = str(x).replace(',', '')
    return float(x)
df['Price'] = df['Price'].apply(f)
df['Mileage'] = df['Mileage'].apply(f)
df.head(5)
ConditionCondition_DescPriceLocationModel_YearMileageExterior_ColorMakeWarrantyModel...Vehicle_TitleOBOFeedback_PercWatch_CountN_ReviewsSeller_StatusVehicle_TileAuctionBuy_NowBid_Count
0Usedmint!!! very low miles11412.0McHenry, Illinois, United States2013.016000.0BlackHarley-DavidsonUnspecifiedTouring...NaNFALSE8.1NaN2427Private SellerClearTrueFALSE28.0
1UsedPerfect condition17200.0Fort Recovery, Ohio, United States2016.060.0BlackHarley-DavidsonVehicle has an existing warrantyTouring...NaNFALSE10017657Private SellerClearTrueTRUE0.0
2UsedNaN3872.0Chicago, Illinois, United States1970.025763.0Silver/BlueBMWVehicle does NOT have an existing warrantyR-Series...NaNFALSE100NaN136NaNClearTrueFALSE26.0
3UsedCLEAN TITLE READY TO RIDE HOME6575.0Green Bay, Wisconsin, United States2009.033142.0RedHarley-DavidsonNaNTouring...NaNFALSE100NaN2920DealerClearTrueFALSE11.0
4UsedNaN10000.0West Bend, Wisconsin, United States2012.017800.0BlueHarley-DavidsonNO WARRANTYTouring...NaNFALSE10013271OWNERClearTrueTRUE0.0

5 rows × 22 columns

df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7493 entries, 0 to 7492
Data columns (total 22 columns):
Condition         7493 non-null object
Condition_Desc    1656 non-null object
Price             7493 non-null float64
Location          7491 non-null object
Model_Year        7489 non-null float64
Mileage           7467 non-null float64
Exterior_Color    6778 non-null object
Make              7489 non-null object
Warranty          5108 non-null object
Model             7370 non-null object
Sub_Model         2426 non-null object
Type              6011 non-null object
Vehicle_Title     268 non-null object
OBO               7427 non-null object
Feedback_Perc     6611 non-null object
Watch_Count       3517 non-null object
N_Reviews         7487 non-null object
Seller_Status     6868 non-null object
Vehicle_Tile      7439 non-null object
Auction           7476 non-null object
Buy_Now           7256 non-null object
Bid_Count         2190 non-null float64
dtypes: float64(4), object(18)
memory usage: 1.3+ MB
any(df.duplicated())
True
# 显示重复数据
# df[df.duplicated()] 
# 统计重复数据
np.sum(df.duplicated())
1221
# 删除重复值
df.drop_duplicates(inplace=True)
df.columns
Index(['Condition', 'Condition_Desc', 'Price', 'Location', 'Model_Year',
       'Mileage', 'Exterior_Color', 'Make', 'Warranty', 'Model', 'Sub_Model',
       'Type', 'Vehicle_Title', 'OBO', 'Feedback_Perc', 'Watch_Count',
       'N_Reviews', 'Seller_Status', 'Vehicle_Tile', 'Auction', 'Buy_Now',
       'Bid_Count'],
      dtype='object')
# 根据指定变量判断重复值
df.drop_duplicates(subset=['Condition', 'Condition_Desc', 'Price', 'Location'], inplace=True)
# 重复已经被删除
np.sum(df.duplicated())
0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值