【问题标题】:Pandas: get values from columns熊猫:从列中获取值
【发布时间】:2021-07-21 13:07:04
【问题描述】:

我在 pandas 中有以下数据框:

               US46434V7617  US3160928731  US4642865251
2021-07-20     13.741297     53.793367    104.151499

如何将其转换为以列为键、以列值为值的字典。例如:

[{'US46434V7617': 13.741297048948578, 'US3160928731': 53.7933674972021, 'US4642865251': 104.15149908700006}]

【问题讨论】:

  • 索引怎么样。 . .你在找df.to_dict(orient='index')吗?还是你想要orient='records'
  • 在进行编辑之前,请注意您想要获得的是 set 的 3 个 dictionaries。因为字典是unhashable,所以python中不存在这样的对象。

标签: python pandas dataframe


【解决方案1】:

您可以将 df.to_dict 与 orient='records' 一起使用:

df.to_dict(orient='records')

这将为您提供一个字典列表,每行一个。顺便说一句,您在问题中提供的结构无效,它必须是字典列表或带有键:值对的字典

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 2022-01-11
    • 2022-01-25
    • 2020-09-23
    • 2017-11-03
    • 2021-07-06
    • 1970-01-01
    • 2022-07-21
    相关资源
    最近更新 更多