Pandas重復(fù)記錄處理
1 概述
Pandas提供了duplicated、Index.duplicated、drop_duplicates函數(shù)來(lái)標(biāo)記及刪除重復(fù)記錄。
duplicated函數(shù)用于標(biāo)記Series中的值、DataFrame中的記錄行是否是重復(fù),重復(fù)為True,不重復(fù)為False。
函數(shù)定義:
pandas.DataFrame.duplicated(self, subset=None, keep='first')pandas.Series.duplicated(self, keep='first')
其中參數(shù)解釋如下:
subset:用于識(shí)別重復(fù)的列標(biāo)簽或列標(biāo)簽序列,默認(rèn)所有列標(biāo)簽keep='first':除了第一次出現(xiàn)外,其余相同的被標(biāo)記為重復(fù)keep='last':除了最后一次出現(xiàn)外,其余相同的被標(biāo)記為重復(fù)keep=False:所有相同的都被標(biāo)記為重復(fù)
2 標(biāo)記DataFrame重復(fù)例子
# 引入numpy和pandas
import numpy as np
import pandas as pd
df = pd.DataFrame({'col1': ['one', 'one', 'two', 'two', 'two', 'three', 'four'], 'col2': [1, 2, 1, 2, 1, 1, 1],
'col3':['AA','BB','CC','DD','EE','FF','GG']},index=['a', 'a', 'b', 'c', 'b', 'a','c'])
df
| (index) | col1 | col2 | col3 |
|---|---|---|---|
| a | one | 1 | AA |
| a | one | 2 | BB |
| b | two | 1 | CC |
| c | two | 2 | DD |
| b | two | 1 | EE |
| a | three | 1 | FF |
| c | four | 1 | GG |
2.1 根據(jù)列名標(biāo)記
#keep='first'
df.duplicated()#默認(rèn)所有列,無(wú)重復(fù)記錄
a False
a False
b False
c False
b False
a False
c False
dtype: bool
#keep='first'
df.duplicated('col1')#第二、四、五行被標(biāo)記為重復(fù)
a False
a True
b False
c True
b True
a False
c False
dtype: bool
#keep='first'
df.duplicated(['col1','col2'])#第五行被標(biāo)記為重復(fù)
a False
a False
b False
c False
b True
a False
c False
dtype: bool
#keep='last'
df.duplicated('col1','last')#第一、三、四行被標(biāo)記重復(fù)
a True
a False
b True
c True
b False
a False
c False
dtype: bool
df.duplicated(['col1','col2'],keep='last')#第三行被標(biāo)記為重復(fù)
a False
a False
b True
c False
b False
a False
c False
dtype: bool
#keep=False
df.duplicated('col1',False)
a True
a True
b True
c True
b True
a False
c False
dtype: bool
#keep=False
df.duplicated(['col1','col2'],keep=False)
a False
a False
b True
c False
b True
a False
c False
dtype: bool
type(df.duplicated(['col1','col2'],keep=False))
pandas.core.series.Series
2.2 根據(jù)索引標(biāo)記
df.index.duplicated()#默認(rèn)keep='first',第二、五、七行被標(biāo)記為重復(fù)
array([False, True, False, False, True, True, True])
df.index.duplicated(keep='last')#第一、二、三、四被標(biāo)記為重復(fù)
array([ True, True, True, True, False, False, False])
df[df.index.duplicated()]#獲取重復(fù)記錄行
| (index) | col1 | col2 | col3 |
|---|---|---|---|
| a | one | 2 | BB |
| b | two | 1 | EE |
| a | three | 1 | FF |
| c | four | 1 | GG |
df[~df.index.duplicated('last')]#獲取不重復(fù)記錄行
| (index) | col1 | col2 | col3 |
|---|---|---|---|
| b | two | 1 | EE |
| a | three | 1 | FF |
| c | four | 1 | GG |
3 標(biāo)記Series重復(fù)例子
s = pd.Series(['one', 'one', 'two', 'two', 'two', 'three', 'four'] ,index= ['a', 'a', 'b', 'c', 'b', 'a','c'],name='sname')
s
a one
a one
b two
c two
b two
a three
c four
Name: sname, dtype: object
s.duplicated()
a False
a True
b False
c True
b True
a False
c False
Name: sname, dtype: bool
s.duplicated('last')
a True
a False
b True
c True
b False
a False
c False
Name: sname, dtype: bool
s.duplicated(False)
a True
a True
b True
c True
b True
a False
c False
Name: sname, dtype: bool
s.index.duplicated()
array([False, True, False, False, True, True, True])
s.index.duplicated('last')
array([ True, True, True, True, False, False, False])
s.index.duplicated(False)
array([ True, True, True, True, True, True, True])