Home  >  Article  >  Backend Development  >  What is the python deduplication function?

What is the python deduplication function?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-10-22 15:19:448687browse

What is the python deduplication function?

Data deduplication can use two methods: duplicated() and drop_duplicates().

DataFrame.duplicated(subset=None, keep='first') returns a boolean Series representing duplicate rows

Parameters:

subset: column label or label sequence, optional

Only certain columns are considered for identifying duplicates, by default all columns are used

keep: {'first', 'last', False}, default 'first'

first: Mark duplicates, True except for the first occurrence.

last: Marks duplicates, True except for the last occurrence.

Error: Mark all duplicates as True.

Related recommendations: "Python Basic Tutorial"

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
df = pd.read_csv('./demo_duplicate.csv')
print(df)
print(df['Seqno'].unique()) # [0. 1.]
# 使用duplicated 查看重复值
# 参数 keep 可以标记重复值 {'first','last',False}
print(df['Seqno'].duplicated())
'''
0    False
1     True
2     True
3     True
4    False
Name: Seqno, dtype: bool
'''
# 删除 series 重复数据
print(df['Seqno'].drop_duplicates())
'''
0    0.0
4    1.0
Name: Seqno, dtype: float64
'''
# 删除 dataframe 重复数据
print(df.drop_duplicates(['Seqno'])) # 按照 Seqno 来去重
'''
   Price     Seqno   Symbol   time
0  1623.0    0.0   APPL  1473411962
4  1649.0    1.0   APPL  1473411963
'''
# drop_dujplicates() 第二个参数 keep 包含的值 有: first、last、False
print(df.drop_duplicates(['Seqno'], keep='last')) # 保存最后一个
'''
   Price     Seqno   Symbol   time
3  1623.0    0.0   APPL  1473411963
4  1649.0    1.0   APPL  1473411963
'''

The above is the detailed content of What is the python deduplication function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to enter π in pythonNext article:How to enter π in python