将 Pandas DataFrame 转换为字典
在许多数据操作任务中,需要将 Pandas DataFrame 中的数据提取为方便的格式。一个常见的需求是将 DataFrame 转换为 Python 字典,其中第一列的元素成为键,同一行中其他列的元素成为值。
示例
考虑以下 DataFrame:
ID A B C 0 p 1 3 2 1 q 4 3 2 2 r 4 0 9
我们希望将此 DataFrame 转换为以下内容的字典形式:
{'p': [1,3,2], 'q': [4,3,2], 'r': [4,0,9]}
解决方案
为了实现这一点,我们可以利用 Pandas 提供的 to_dict() 方法。但是,为了将 DataFrame 对齐为所需的格式,我们需要将“ID”列设置为索引并使用 .T 转置 DataFrame。此外,我们在 to_dict() 中指定 orient 参数来输出每列的值列表。
以下代码演示了这种方法:
df.set_index('ID').T.to_dict('list')
这将生成所需的字典:
{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}
替代选项
除了默认的字典格式之外,Pandas 还提供了一系列使用 orient 参数输出字典的选项:
- dict: 列名作为键,值作为索引的字典:数据对
- 列表: 键是列名,值是列列表data
- series: 与 'list' 类似,但值是 Series 对象
- split: 列名作为键,数据值作为值,和索引标签作为单独的键
- 记录:每一行都成为一个字典列名作为键,数据作为值
- index: 与“records”类似,但是字典的字典,索引标签作为键
以上是如何将 Pandas DataFrame 转换为具有列表值的字典?的详细内容。更多信息请关注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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

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

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

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

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。