Python?pandas找出、刪除重復(fù)的數(shù)據(jù)實例
前言
當(dāng)我們使用pandas處理數(shù)據(jù)的時候,經(jīng)常會遇到數(shù)據(jù)重復(fù)的問題,如何找出重復(fù)數(shù)據(jù)進而分析重復(fù)原因,或者如何直接刪除重復(fù)的數(shù)據(jù)是一個關(guān)鍵的步驟,pandas提供了很方便的方法:duplicated()和drop_duplicates()。
一、duplicated()
duplicated()可以被用在DataFrame的三種情況下,分別是pandas.DataFrame.duplicated、pandas.Series.duplicated和pandas.Index.duplicated。他們的用法都類似,前兩個會返回一個布爾值的Series,最后一個會返回一個布爾值的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ù)的項都標(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ù)值給刪掉。下面只會介紹一些含義不同的參數(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()也是多了一個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)實,都是在實際項目中不斷的發(fā)現(xiàn)、積累、記錄下來。
到此這篇關(guān)于Python pandas找出、刪除重復(fù)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)pandas找出刪除重復(fù)數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python計時相關(guān)操作詳解【time,datetime】
這篇文章主要介紹了Python計時相關(guān)操作,涉及time,datetime模塊的使用技巧,包括時間戳、時間差、日期格式等操作方法,需要的朋友可以參考下2017-05-05Python小紅書旋轉(zhuǎn)驗證碼識別實戰(zhàn)教程
這篇文章主要介紹了Python小紅書旋轉(zhuǎn)驗證碼識別實戰(zhàn)教程,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-08-08Python成功解決讀文件出現(xiàn):IOError:?[Errno?0]?Error的錯誤
在Python編程中,處理文件是常見的任務(wù)之一,但偶爾也會遇到各種錯誤,包括IOError,盡管Python?3.x中IOError已被OSError和FileNotFoundError等更具體的異常所取代,由于[Errno?0]不直接指向具體的錯誤類型,我們將討論一系列可能導(dǎo)致IOError的常見情況,需要的朋友可以參考下2024-07-07Python接口測試數(shù)據(jù)庫封裝實現(xiàn)原理
這篇文章主要介紹了Python接口測試數(shù)據(jù)庫封裝實現(xiàn)原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05python代碼打印100-999之間的回文數(shù)示例
今天小編就為大家分享一篇python代碼打印100-999之間的回文數(shù)示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11