Pandas重復(fù)記錄處理

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])

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容