在 Pandas 中透视 Dataframe
该任务涉及转置 CSV 格式的数据表,其中“指标”列值变为新的专栏。所需的结果是扁平格式,其中行由“国家”和“年份”定义,列是“指标”值。
使用 .pivot
要实现枢轴操作,可以利用 .pivot 方法作为如下所示:
out = df.pivot(index=['Country', 'Year'], columns='Indicator', values='Value')
此方法重新排列数据,使“国家”和“年份”成为行索引,“指标”值成为列。生成的“out”变量保存透视数据。
要将数据返回为平面表格式,可以使用 .rename_axis 从列中消除“Indicator”标签,并使用 .reset_index 恢复“Country”和“年份”作为列。
print(out.rename_axis(columns=None).reset_index())
这会生成一个带有“国家/地区”的扁平表格, “年份”和“指标”值作为列。
使用 .pivot_table
如果“国家/地区”、“年份”和“指标”重复' 原始数据集中的组合,可以使用.pivot_table。它对重复值执行聚合(默认平均值)。
out = df.pivot_table( index=['Country', 'Year'], columns='Indicator', values='Value') print(out.rename_axis(columns=None).reset_index())
此方法会生成一个扁平化的表格,其中对重复值进行平均,并且列中省略了“指标”标签。
相关文档
有关重塑和数据透视表的更多详细信息,请参阅Pandas,请参阅以下资源:
- 重塑和数据透视表用户指南
- 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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

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

WebStorm Mac版
好用的JavaScript开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)