本文主要介紹了pandas統(tǒng)計(jì)重復(fù)值次數(shù)的方法實(shí)現(xiàn),分享給大家,具體如下:
from pandas import DataFrame df = DataFrame({'key1':['a','a','b','b','a','a'], 'key2':['one','two','one','two','one','one'], 'data1':[1,2,3,2,1,1], # 'data2':np.random.randn(5) }) # 打印數(shù)據(jù)框 print(df) # data1 key1 key2 # 0 1 a one # 1 2 a two # 2 3 b one # 3 2 b two # 4 1 a one # 5 1 a one # 重復(fù)項(xiàng) print(df[df.duplicated()]) # data1 key1 key2 # 4 1 a one # 5 1 a one # 統(tǒng)計(jì)重復(fù)值 dup=df[df.duplicated()].count() print(dup) # 最后兩項(xiàng)重復(fù) # data1 2 # key1 2 # key2 2 # 去除重復(fù)項(xiàng) nodup=df[-df.duplicated()] print(nodup) # data1 key1 key2 # 0 1 a one # 1 2 a two # 2 3 b one # 3 2 b two
方法有二:
1. 在調(diào)用duplicated方法后,非重復(fù)的元素會(huì)被標(biāo)記為False,而重復(fù)的元素會(huì)被標(biāo)記為T(mén)rue
count = 0 for i in users_info['user_id'].duplicated(): if i == True: count = count + 1 count
【注1】users_info為一個(gè)dataframe框,user_id為其中一列
【注2】duplicated( )方法只會(huì)把重復(fù)的元素標(biāo)記為T(mén)rue,而不會(huì)標(biāo)記被重復(fù)的元素
2.這行代碼的速度更快,drop_duplicates(['user_id'])方法為刪除user_id列中相同的元素
users_info.shape[0] - users_info.drop_duplicates(['user_id']).shape[0]
【注】shape[0] 為獲取行數(shù)
到此這篇關(guān)于pandas統(tǒng)計(jì)重復(fù)值次數(shù)的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)pandas統(tǒng)計(jì)重復(fù)值次數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:郴州 烏蘭察布 海南 合肥 哈爾濱 烏蘭察布 大慶 平頂山
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pandas統(tǒng)計(jì)重復(fù)值次數(shù)的方法實(shí)現(xiàn)》,本文關(guān)鍵詞 pandas,統(tǒng)計(jì),重復(fù),值,次數(shù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。