使用字典提高 Pandas 系列中值替換的表現
使用字典替換 Pandas 系列中的值是一項常見任務。雖然建議使用 s.replace(d) 替換值,但它可能比使用簡單的列表理解慢得多。
表現緩慢的原因
表現緩慢s.replace(d) 的作用源自於它對邊緣情況和罕見情況的處理。它涉及:
- 將字典轉換為列表。
- 迭代列表並檢查巢狀字典。
- 將鍵和值的迭代器輸入到替換功能。
替代方法
要提高效能,請考慮使用以下方法:
- 完整地圖:
- 完整地圖: >如果所有值都在系列由字典映射。此方法高效且始終更快。
部分映射:如果字典僅映射一小部分(例如,小於5%)值,請使用s.map(d ).fillna(s['A']). astype(int).這種方法將映射與填充相結合,避免了昂貴的迭代。
基準測試##### Full Map ##### d = {i: i+1 for i in range(1000)} %timeit df['A'].replace(d) # Slow (1.98s) %timeit df['A'].map(d) # Fast (84.3ms) ##### Partial Map ##### d = {i: i+1 for i in range(10)} %timeit df['A'].replace(d) # Intermediate (20.1ms) %timeit df['A'].map(d).fillna(df['A']).astype(int) # Faster (111ms)基準測試展示了s.replace(d), s 之間的性能差異.map(d) 和列表理解:
這表示對於完整或部分映射,s.map(d) 始終比s.replace(d) 更快。
結論取決於字典覆蓋的完整性, s.map(d) 或s.map(d).fillna(s['A']) .astype(int) 應該優先於s.replace(d) 用於Pandas 系列中的高效值替換。以上是為什麼使用字典替換 Pandas 系列中的值很慢,如何提升效能?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

是的,YouCanconCatenatElistsusingAloopInpyThon.1)使用eparateLoopsForeachListToAppendIteMstoaresultList.2)useanestedlooptoiterateOverMultipliplipliplipliplipliplipliplipliplipliplistforamoreConciseApprace.3)

ThemostefficientmethodsforconcatenatinglistsinPythonare:1)theextend()methodforin-placemodification,2)itertools.chain()formemoryefficiencywithlargedatasets.Theextend()methodmodifiestheoriginallist,makingitmemory-efficientbutrequirescautionifpreserving

pythonboopsincludeforandwhileloops,with forloopsidealforequencessand and whileloopsforcondition repetition.bestpracticesinvolve:1)使用listComprehensionsforshensionsforsimpletranspletransformations,2)obseringEnumerateForIndex-valuepairs,3)optingftingftingfortermornemoremoremoremore


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

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

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

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

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