将 Pandas MultiIndex DataFrame 转换回原始形式
使用 Pandas DataFrame 时,通常会执行分组操作来聚合数据。然而,分组后,生成的 DataFrame 可能具有多索引层次结构,这可能具有挑战性。本文讨论了一种将多索引 DataFrame 转换回其原始形式的方法,并使用示例 DataFrame 进行了简单演示。
问题
给定的示例DataFrame 包含多行数据,其中包含“城市”和“名称”列。我们对 DataFrame 执行 GroupBy 操作,使用 count() 函数按“名称”和“城市”进行聚合。生成的分组 DataFrame 具有多索引 ("Name", "City")。
解决方案
将多索引 DataFrame 转换回其原始形式,我们可以使用add_suffix()和reset_index()函数。 add_suffix() 函数为列名添加后缀,reset_index() 函数将多索引转换为单索引 DataFrame。
g1.add_suffix('_Count').reset_index()
生成的 DataFrame 将包含原始行以及附加的行列“_Count”表示“名称”和“城市”每个组合的计数。
替代方法
转换多索引DataFrame的另一种方法是使用DataFrame()函数和size()函数创建一个新的DataFrame来计算“Name”和“的每个组合的行数City"。
DataFrame({'count' : df1.groupby( [ "Name", "City"] ).size()}).reset_index()
此方法不需要使用 add_suffix() 函数,但会生成具有单个“count”列的 DataFrame而不是为多索引的每个级别单独的计数列。
通过利用这些方法,可以轻松地将多索引 DataFrame 转换回其原始形式,从而促进进一步的数据操作和分析任务。
以上是如何将 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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

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

Dreamweaver Mac版
视觉化网页开发工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

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

SublimeText3 英文版
推荐:为Win版本,支持代码提示!