使用字典提高 Pandas 系列中值替换的性能
使用字典替换 Pandas 系列中的值是一项常见任务。虽然建议使用 s.replace(d) 替换值,但它可能比使用简单的列表理解要慢得多。
性能缓慢的原因
性能缓慢s.replace(d) 的作用源于它对边缘情况和罕见情况的处理。它涉及:
- 将字典转换为列表。
- 迭代列表并检查嵌套字典。
- 将键和值的迭代器输入到替换功能。
替代方法
要提高性能,请考虑使用以下方法:
- 完整地图:如果所有值都在系列由字典映射。此方法高效且始终更快。
- 部分映射:如果字典仅映射一小部分(例如,小于 5%)值,请使用 s.map(d ).fillna(s['A']).astype(int).这种方法将映射与填充相结合,避免了昂贵的迭代。
基准测试
基准测试展示了 s.replace(d), s 之间的性能差异.map(d) 和列表理解:
##### 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.map(d) 始终比 s.replace(d) 更快。
结论
取决于字典覆盖的完整性, s.map(d) 或 s.map(d).fillna(s['A']).astype(int) 应该优先于s.replace(d) 用于 Pandas 系列中的高效值替换。
以上是为什么使用字典替换 Pandas 系列中的值很慢,如何提高性能?的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

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)

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

禅工作室 13.0.1
功能强大的PHP集成开发环境

WebStorm Mac版
好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具