濮阳杆衣贸易有限公司

主頁 > 知識庫 > 聊聊python中令人迷惑的duplicated和drop_duplicates()用法

聊聊python中令人迷惑的duplicated和drop_duplicates()用法

熱門標(biāo)簽:沈陽防封電銷卡品牌 江西省地圖標(biāo)注 武漢外呼系統(tǒng)平臺 池州外呼調(diào)研線路 沈陽人工外呼系統(tǒng)價格 如何申請400電話費用 沈陽外呼系統(tǒng)呼叫系統(tǒng) 富錦商家地圖標(biāo)注 外呼系統(tǒng)哪些好辦

前言

在算face_track_id map有感:

開始驗證

data={'state':[1,1,2,2,1,2,2,2],'pop':['a','b','c','d','b','c','d','d']}
frame=pd.DataFrame(data)  
frame

frame.shape
$ (8,2)
# 說明duplicated()是對整行進(jìn)行查重,return 重復(fù)了的數(shù)據(jù),且只現(xiàn)實n-1條重復(fù)的數(shù)據(jù)(n是重復(fù)的次數(shù))
frame[frame.duplicated() == True]

一開始還很疑惑,明明(1,b)只出現(xiàn)了1次,哪里duplicate了。其實,人家return的結(jié)果是去掉已經(jīng)出現(xiàn)過一次的行數(shù)據(jù)了。所以看起來有點confuse,感覺(1,b)并沒有重復(fù),但其實人家的函數(shù)很簡潔呢,返回了重復(fù)值而且不冗余。

# 說明drop_duplicates()函數(shù)是將所有重復(fù)的數(shù)據(jù)都去掉了,且默認(rèn)保留重復(fù)數(shù)據(jù)的第一條。
# 比如(2,d)出現(xiàn)了3次,在duplicated()中顯示了2次,在drop_dupicates()后保留了一個
frame.drop_duplicates().shape
$ (4,2)
# 留下了完全唯一的數(shù)據(jù)行
frame.drop_duplicates()

補充:python的pandas重復(fù)值處理(duplicated()和drop_duplicates())

一、生成重復(fù)記錄數(shù)據(jù)

import numpy as np
import pandas as pd
 
#生成重復(fù)數(shù)據(jù)
df=pd.DataFrame(np.ones([5,2]),columns=['col1','col2'])
df['col3']=['a','b','a','c','d']
df['col4']=[3,2,3,2,2]
df=df.reindex(columns=['col3','col4','col1','col2'])  #將新增的一列排在第一列
df

輸出:

二、判斷重復(fù)記錄(行)

#判斷重復(fù)數(shù)據(jù)
isDplicated=df.duplicated()   #判斷重復(fù)數(shù)據(jù)記錄
isDplicated

輸出:

三、刪除重復(fù)值

#刪除重復(fù)值
new_df1=df.drop_duplicates() #刪除數(shù)據(jù)記錄中所有列值相同的記錄
new_df2=df.drop_duplicates(['col3']) #刪除數(shù)據(jù)記錄中col3列值相同的記錄
new_df3=df.drop_duplicates(['col4']) #刪除數(shù)據(jù)記錄中col4列值相同的記錄
new_df4=df.drop_duplicates(['col3','col4']) #刪除數(shù)據(jù)記錄中(col3和col4)列值相同的記錄
new_df1
new_df2
new_df3
new_df4

輸出:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Python DataFrame使用drop_duplicates()函數(shù)去重(保留重復(fù)值,取重復(fù)值)
  • Pandas之drop_duplicates:去除重復(fù)項方法
  • 詳解pandas使用drop_duplicates去除DataFrame重復(fù)項參數(shù)
  • pandas.DataFrame.drop_duplicates 用法介紹

標(biāo)簽:潛江 銅川 常德 株洲 通遼 黑龍江 呂梁 阿里

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《聊聊python中令人迷惑的duplicated和drop_duplicates()用法》,本文關(guān)鍵詞  聊聊,python,中,令人,迷惑,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《聊聊python中令人迷惑的duplicated和drop_duplicates()用法》相關(guān)的同類信息!
  • 本頁收集關(guān)于聊聊python中令人迷惑的duplicated和drop_duplicates()用法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    襄城县| 福清市| 南川市| 淳化县| 固始县| 安乡县| 德兴市| 磐石市| 自贡市| 阿克苏市| 浠水县| 丰台区| 湖南省| 陆良县| 卫辉市| 芦山县| 金川县| 宣威市| 洛南县| 东乡县| 洛浦县| 凤凰县| 阳朔县| 农安县| 陆良县| 永修县| 华亭县| 义马市| 陆河县| 峨边| 辽宁省| 卓尼县| 梅河口市| 新安县| 酉阳| 南江县| 全南县| 沁阳市| 湘潭市| 洛浦县| 德江县|