使用Pandas 計算組中的唯一值
在處理包含分組到類別中的多個變數的資料集時,通常有必要確定數量與每個組相關的唯一值。 Pandas 是一種廣泛使用的用於資料操作的 Python 庫,它提供了多種方法來計算群組內的唯一值。
一個常見的需求是計算每個域內唯一識別碼的數量。給定一個包含 ID 和域列的資料框,我們尋求獲得顯示每個域的唯一 ID 計數的結果。
具體而言,考慮資料:
ID domain 0 123 vk.com 1 123 vk.com 2 123 twitter.com 3 456 vk.com 4 456 facebook.com 5 456 vk.com 6 456 google.com 7 789 twitter.com 8 789 vk.com
我們目標是實現以下輸出:
domain count vk.com 3 twitter.com 2 facebook.com 1 google.com 1
為了實現這一點,我們可以在Pandas groupby 操作中使用nunique() 函數。透過按域列對資料框進行分組,然後將 nunique() 函數套用至 ID 列,我們可以獲得每個域的唯一值的計數。產生的資料框將包含所需的結果:
df = df.groupby(['domain', 'ID']).nunique() print(df)
但是,在某些情況下,資料可能包含網域名稱中的單引號等字元。為了處理這種情況,我們可以利用 str.strip("'") 函數在分組和計數之前刪除單引號。這可以實現為:
df = df.ID.groupby([df.domain.str.strip("'")]).nunique() print(df)
或者,我們可以透過在groupby 操作中連結str.strip("'") 函數來簡化程式碼:
df.groupby(df.domain.str.strip("'"))['ID'].nunique()
保留結果資料框中的域列,我們可以使用帶有as_index=False 參數的agg() 函數:
df = df.groupby(by='domain', as_index=False).agg({'ID': pd.Series.nunique}) print(df)
此方法將傳回一個包含域和計數列的資料框,其中count表示與每個域關聯的唯一ID 的數量。
以上是如何計算熊貓群體中的唯一值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增強效率和通用性。

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

在Python中,可以通過多種方法連接列表並管理重複元素:1)使用 運算符或extend()方法可以保留所有重複元素;2)轉換為集合再轉回列表可以去除所有重複元素,但會丟失原有順序;3)使用循環或列表推導式結合集合可以去除重複元素並保持原有順序。

fasteStmethodMethodMethodConcatenationInpythondependersonListsize:1)forsmalllists,operatorseffited.2)forlargerlists,list.extend.extend()orlistComprechensionfaster,withextendEffaster,withExtendEffers,withextend()withextend()是extextend()asmoremory-ememory-emmoremory-emmoremory-emmodifyinginglistsin-place-place-place。

toInSerteLementIntoApythonList,useAppend()toaddtotheend,insert()foreSpificPosition,andextend()formultiplelements.1)useappend()foraddingsingleitemstotheend.2)useAddingsingLeitemStotheend.2)useeapecificindex,toadapecificindex,toadaSpecificIndex,toadaSpecificIndex,blyit'ssssssslorist.3 toaddextext.3

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他們areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)刪除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

toresolvea“ dermissionded”錯誤Whenrunningascript,跟隨台詞:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版