栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pandas中数据去重

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pandas中数据去重

背景

在数据处理过程中常常会遇到重复的问题,这里简要介绍遇到过的数据重复问题及其如何根据具体的需求进行处理。

筛选出指定字段存在重复的数据
import pandas as pd

student_dict = {"name": ["Joe", "Nat", "Harry", "Nat"], "age": [20, 21, 19, 21], "marks": [85.10, 77.80, 91.54, 77.80]}

# Create Dataframe from dict
student_df = pd.Dataframe(student_dict)
print(student_df)

duplicated_task_df = student_df[student_df.duplicated(subset=["age"], keep=False)]
print("duplicated_task_df:")
print(duplicated_task_df)

运行结果如下:

一旦重复即全部删除

一旦出现重复,则相同数据全部删除,一般出现在文本相同,但是标签不一致的场景:

import pandas as pd

student_dict = {"name": ["Joe", "Nat", "Harry", "Nat"], "age": [20, 21, 19, 21], "marks": [85.10, 77.80, 91.54, 77.80]}

# Create Dataframe from dict
student_df = pd.Dataframe(student_dict)
print(student_df)

# drop all duplicate rows
student_df = student_df.drop_duplicates(keep=False)
print("drop all duplicate rows:")
print(student_df)

运行结果如下:

    name  age  marks
0    Joe   20  85.10
1    Nat   21  77.80
2  Harry   19  91.54
3    Nat   21  77.80
drop all duplicate rows:
    name  age  marks
0    Joe   20  85.10
2  Harry   19  91.54
原地操作

上述的去重操作结果是以一个copy出来的新Dataframe,这也是Dataframe.drop_duplicates的默认行为。如果想要直接在现有的Dataframe上进行修改,设置inplace=True即可。

import pandas as pd

student_dict = {"name": ["Joe", "Nat", "Harry", "Joe", "Nat"], "age": [20, 21, 19, 20, 21],
                "marks": [85.10, 77.80, 91.54, 85.10, 77.80]}

# Create Dataframe from dict
student_df = pd.Dataframe(student_dict)
print(student_df)

# drop duplicate rows
student_df.drop_duplicates(inplace=True)
print("drop duplicate rows with inplace=True:")
print(student_df)

运行结果如下:

    name  age  marks
0    Joe   20  85.10
1    Nat   21  77.80
2  Harry   19  91.54
3    Joe   20  85.10
4    Nat   21  77.80
drop duplicate rows with inplace=True:
    name  age  marks
0    Joe   20  85.10
1    Nat   21  77.80
2  Harry   19  91.54
根据指定字段去重后,并重置index

Dataframe.drop_duplicates 默认情况下是保留原始的row index,但是有时候我们需要根据0-N这种等差递增的index做其他操作时候,则需要重置index。

当 ignore_index=True, 重置行index为 0, 1, …, n – 1.
当 ignore_index=False, 则保留原始的行index, 这是默认操作

示例如下:

import pandas as pd

student_dict = {"name": ["Joe", "Nat", "Harry", "Nat"], "age": [20, 21, 19, 21], "marks": [85.10, 77.80, 91.54, 77.80]}

# Create Dataframe from dict
student_df = pd.Dataframe(student_dict, index=['a', 'b', 'c', 'd'])
print(student_df)

# drop duplicate rows
student_df0 = student_df.drop_duplicates(keep=False)
print("drop duplicate rows with ignore_index=False:")
print(student_df0)

# drop duplicate rows
student_df1 = student_df.drop_duplicates(keep=False, ignore_index=True)
print("drop duplicate rows with ignore_index=True:")
print(student_df1)

运行结果如下:

    name  age  marks
a    Joe   20  85.10
b    Nat   21  77.80
c  Harry   19  91.54
d    Nat   21  77.80
drop duplicate rows with ignore_index=False:
    name  age  marks
a    Joe   20  85.10
c  Harry   19  91.54
drop duplicate rows with ignore_index=True:
    name  age  marks
0    Joe   20  85.10
1  Harry   19  91.54
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/275177.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号