如何使用Pandas Groupby 取得字串並集
使用Pandas 的groupby 方法將資料分組時,可以使用以下方式輕鬆聚合數字列類似sum 的函數。然而,聚合字串列帶來了挑戰,因為並不總是需要簡單的串聯。本文探討了取得群組內字串並集的方法。
問題:
考慮以下DataFrame:
A | B | C |
---|---|---|
1 | 0.749065 | This |
2 | 0.301084 | is |
3 | 0.463468 | a |
4 | 0.643961 | random |
1 | 0.866521 | string |
2 | 0.120737 | ! |
應用df. groupby("A")["B"].sum( ) 傳回每組B 列中數值的總和。但是,在字串列 C 上呼叫 df.groupby("A")["C"].sum() 無法如預期運作,導致字串串聯。
解決方案:
自訂函數:
一種方法是定義一個自訂函數來聚合組內的字串值。然後可以使用 apply() 方法將此函數套用到 DataFrame。例如:
<code class="python">def f(x): return Series(dict(A = x['A'].sum(), B = x['B'].sum(), C = "{%s}" % ', '.join(x['C']))) df.groupby('A').apply(f)</code>
這將傳回一個 DataFrame,其中每個群組的 C 列中的字串並集,其中字串包含在大括號內。
帶有. sum():
另一種方法是將lambda 函數應用於groupby 對象,對數字列使用.sum() ,對字串列使用自訂串聯:
<code class="python">df.groupby('A').apply(lambda x: x.sum())</code>
這將傳回一個包含數值和連接字串總和的DataFrame。要獲得字串的並集,您可以在 lambda 函數中使用字串操作。
效能注意事項:
需要注意的是,將自訂函數應用於 groupby物件比在數字列上使用聚合函數慢。對於大型資料集,應該考慮這種效能權衡。
以上是如何組合 Pandas Groupby 中的字串以獲得唯一值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

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

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

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