Python?pandas找出、刪除重復(fù)的數(shù)據(jù)實(shí)例
前言
當(dāng)我們使用pandas處理數(shù)據(jù)的時(shí)候,經(jīng)常會(huì)遇到數(shù)據(jù)重復(fù)的問題,如何找出重復(fù)數(shù)據(jù)進(jìn)而分析重復(fù)原因,或者如何直接刪除重復(fù)的數(shù)據(jù)是一個(gè)關(guān)鍵的步驟,pandas提供了很方便的方法:duplicated()和drop_duplicates()。
一、duplicated()
duplicated()可以被用在DataFrame的三種情況下,分別是pandas.DataFrame.duplicated、pandas.Series.duplicated和pandas.Index.duplicated。他們的用法都類似,前兩個(gè)會(huì)返回一個(gè)布爾值的Series,最后一個(gè)會(huì)返回一個(gè)布爾值的numpy.ndarray。
DataFrame.duplicated(subset=None, keep=‘first’)
subset:默認(rèn)為None,需要標(biāo)記重復(fù)的標(biāo)簽或標(biāo)簽序列
keep:默認(rèn)為‘first’,如何標(biāo)記重復(fù)標(biāo)簽
- first:將除第一次出現(xiàn)以外的重復(fù)數(shù)據(jù)標(biāo)記為True
- last:將除最后一次出現(xiàn)以外的重復(fù)數(shù)據(jù)標(biāo)記為True
- False:將所有重復(fù)的項(xiàng)都標(biāo)記為True(不管是不是第一次出現(xiàn))
Series.duplicated(keep=‘first’)
keep:與DataFrame.duplicated的keep相同
Index.duplicated(keep=‘first’)
keep:與DataFrame.duplicated的keep相同
例子:
import pandas as pd df = pd.DataFrame({ 'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'], 'style': ['cup', 'cup', 'cup', 'pack', 'pack'], 'rating': [4, 4, 3.5, 15, 5] }) df
brand style rating
0 Yum Yum cup 4.0
1 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
df.duplicated()
0 False
1 True
2 False
3 False
4 False
dtype: bool
df.duplicated(keep='last')
0 True
1 False
2 False
3 False
4 False
dtype: bool
df.duplicated(keep=False)
0 True
1 True
2 False
3 False
4 False
dtype: bool
df.duplicated(subset=['brand'])
0 False
1 True
2 False
3 True
4 True
dtype: bool
關(guān)于Index的重復(fù)標(biāo)記:
df = df.set_index('brand') df
style rating
brand
Yum Yum cup 4.0
Yum Yum cup 4.0
Indomie cup 3.5
Indomie pack 15.0
Indomie pack 5.0
df.index.duplicated()
array([False, True, False, True, True])
二、drop_duplicates()
與duplicated()類似,drop_duplicates()是直接把重復(fù)值給刪掉。下面只會(huì)介紹一些含義不同的參數(shù)。
DataFrame.drop_duplicates(subset=None, keep=‘first’, inplace=False)
- subset:與duplicated()中相同
- keep:與duplicated()中相同
- inplace:與pandas其他函數(shù)的inplace相同,選擇是修改現(xiàn)有數(shù)據(jù)還是返回新的數(shù)據(jù)
Series.drop_duplicates()相比Series.duplicated()也是多了一個(gè)inplace參數(shù),和上訴介紹一樣,Index.drop_duplicates()與Index.duplicated()參數(shù)相同就不做贅述。下面是例子:
df = pd.DataFrame({ 'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'], 'style': ['cup', 'cup', 'cup', 'pack', 'pack'], 'rating': [4, 4, 3.5, 15, 5] }) df
brand style rating
0 Yum Yum cup 4.0
1 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
df.drop_duplicates()
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
df.drop_duplicates(inplace = True) df
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
總結(jié)
有剩余無,pandas有很多好用的庫,但是系統(tǒng)學(xué)下來很不現(xiàn)實(shí),都是在實(shí)際項(xiàng)目中不斷的發(fā)現(xiàn)、積累、記錄下來。
到此這篇關(guān)于Python pandas找出、刪除重復(fù)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)pandas找出刪除重復(fù)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+Pygame實(shí)現(xiàn)經(jīng)典魂斗羅游戲
《魂斗羅》(Contra)是由Konami于1987年推出的一系列卷軸射擊類單機(jī)游戲。本文將利用Python中的Pygame庫實(shí)現(xiàn)這一經(jīng)典游戲,感興趣的可以了解一下2022-05-05快速解釋如何使用pandas的inplace參數(shù)的使用
這篇文章主要介紹了快速解釋如何使用pandas的inplace參數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07python操作mysql實(shí)現(xiàn)一個(gè)超市管理系統(tǒng)
超市管理系統(tǒng)有管理員和普通用戶兩條分支,只需掌握Python基礎(chǔ)語法,就可以完成這個(gè)項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于python操作mysql實(shí)現(xiàn)一個(gè)超市管理系統(tǒng)的相關(guān)資料,需要的朋友可以參考下2022-12-12Python?對(duì)象拷貝及深淺拷貝區(qū)別的詳細(xì)教程示例
這篇文章主要介紹了Python?對(duì)象拷貝及深淺拷貝區(qū)別的詳細(xì)教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03