【数据分析可视化】通过去重进行数据清洗

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
# 读取刚刚分解处理完的返回数据
link_csv = '/Users/bennyrhys/Desktop/数据分析可视化-数据集/homework/demo_duplicate.csv'
df = pd.read_csv(link_csv)
df
Unnamed: 0 Price Seqno Symbol time
0 0 1623.0 0.0 APPL 1473411962
1 1 1623.0 0.0 APPL 1473411962
2 2 1623.0 0.0 APPL 1473411963
3 3 1623.0 0.0 APPL 1473411963
4 4 1649.0 1.0 APPL 1473411963
# 删掉无用的unname
del df['Unnamed: 0']
df
Price Seqno Symbol time
0 1623.0 0.0 APPL 1473411962
1 1623.0 0.0 APPL 1473411962
2 1623.0 0.0 APPL 1473411963
3 1623.0 0.0 APPL 1473411963
4 1649.0 1.0 APPL 1473411963
df.size
20
len(df)
5
# 查看no列有多少重复的
df['Seqno'].unique()
array([0., 1.])
len(df['Seqno'].unique())
2
# 检测是否与前边重复
df['Seqno'].duplicated()
0    False
1     True
2     True
3     True
4    False
Name: Seqno, dtype: bool
# 删掉重复的数据也就是上方展示为true的数据
df['Seqno'].drop_duplicates()
0    0.0
4    1.0
Name: Seqno, dtype: float64
# 这样范围局限,无法展示全部(Series)
type(df['Seqno'].drop_duplicates())
pandas.core.series.Series
# 这样no列重复值删不感觉(不传参,则整体考虑某列重复最小处理原则)
df.drop_duplicates()
Price Seqno Symbol time
0 1623.0 0.0 APPL 1473411962
2 1623.0 0.0 APPL 1473411963
4 1649.0 1.0 APPL 1473411963
# 在DataFrame状态下进行处理(暂时全部)
df.drop_duplicates(['Seqno'])
Price Seqno Symbol time
0 1623.0 0.0 APPL 1473411962
4 1649.0 1.0 APPL 1473411963
# 去重 参数(保留最后出现的)
df.drop_duplicates(['Seqno'],keep='last')
Price Seqno Symbol time
3 1623.0 0.0 APPL 1473411963
4 1649.0 1.0 APPL 1473411963
原创文章 257 获赞 187 访问量 16万+

猜你喜欢

转载自blog.csdn.net/weixin_43469680/article/details/105623985
今日推荐