使用Groupby 取得群組中具有最大值的行
在執行資料分析時,通常需要識別具有最高值的行對於由其他列定義的每個組中的特定列。可以使用廣泛使用的 Python 資料操作庫 pandas 的 groupby() 和 transform() 方法方便地執行此操作。
問題陳述
給定一個包含「Sp」、「Mt」、「Value」和「count」等列的pandas DataFrame,我們的目標是提取具有最大值的行由「Sp」和「Mt」列定義的每個群組中的「count」值。
解
要擷取所需的行,我們可以採用以下步驟:
-
計算每個的最大計數分組:
- 利用groupby()方法按 'Sp' 和 'Mt' 欄位將 DataFrame 分組,然後將 max() 函數套用至 'count' 欄位以確定每組的最大計數值。
-
辨識具有最大計數的行:
- 利用transform()方法為每行回傳一個True/False布林系列,其中「True」表示該行在其範圍內具有最大計數值group.
- 使用以下命令檢索與True 值對應的原始DataFrame 行
範例1
考慮以下DataFrame:
Sp | Mt | Value | count |
---|---|---|---|
MM1 | S1 | a | 3 |
MM1 | S1 | n | 2 |
MM1 | S3 | cb | 5 |
MM2 | S3 | mk | 8 |
MM2 | S4 | bg | 10 |
MM2 | S4 | dgd | 1 |
MM4 | S2 | rd | 2 |
MM4 | S2 | cb | 2 |
MM4 | S2 | uyi | 7 |
Sp | Mt | Value | count |
---|---|---|---|
MM1 | S1 | a | 3 |
MM1 | S3 | cb | 5 |
MM2 | S3 | mk | 8 |
MM2 | S4 | bg | 10 |
MM4 | S2 | uyi | 7 |
範例 2
使用不同的 DataFrame:
Sp | Mt | Value | count |
---|---|---|---|
MM2 | S4 | bg | 10 |
MM2 | S4 | dgd | 1 |
MM4 | S2 | rd | 2 |
MM4 | S2 | cb | 8 |
MM4 | S2 | uyi | 8 |
輸出變成:
Sp | Mt | Value | count |
---|---|---|---|
MM2 | S4 | bg | 10 |
MM4 | S2 | cb | 8 |
MM4 | S2 | uyi | 8 |
輸出變成:
輸出變成。 🎜>- 另一種方法是新增一列到表示每個群組的最大計數的 DataFrame。這可以透過以下步驟來實現:
- 使用df.groupby(['Sp', 'Mt'])['count'].max() 計算每個群組的最大計數表達式。
- 使用df['count_max'] = 將一個名為'count_max' 的新欄位加入DataFrame 中df.groupby(['Sp', 'Mt'])['count'].transform( max) 表達式。
以上是如何在 Pandas 中找到組內具有最大值的行?的詳細內容。更多資訊請關注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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

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

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。