【问题标题】:python dictionary to pandas dataframe returns empty dataframepython字典到熊猫数据框返回空数据框
【发布时间】:2017-10-14 21:52:47
【问题描述】:

我的程序中有一个创建运行时的字典,需要转换为 pandas 数据框。

dictio={'a':'abc','b':'zyx','c':'prq'} df=pd.Dataframe(dictio)

上面的代码返回一个空的数据框,其中创建了列。谁能指出我错过了什么?

【问题讨论】:

    标签: python python-3.x pandas dictionary


    【解决方案1】:
    #use from_dict and set orient to index and then transpose in the end.
    pd.DataFrame.from_dict(dictio,orient='index').T
    Out[263]: 
         a    c    b
    0  abc  prq  zyx
    

    【讨论】:

    • 这可行,但我希望结果采用转置格式,因为字典中的键应该是列名。
    • Keys(a,b,c) 是这里的列名。您的预期结果是什么?
    • 我相信我提供的示例还不够,我有一个空字典,其中包含大量列,其值在程序期间设置。转换为数据框让我可以使用建议的字典键作为第一列,使用 0 作为另一列。
    • 我不确定我是否理解你的问题。请提供一个包含更多数据和所需输出的示例。
    猜你喜欢
    • 2020-07-23
    • 2022-07-22
    • 2016-10-17
    • 2017-08-02
    • 2017-02-27
    • 2021-11-10
    • 1970-01-01
    • 2021-07-17
    • 2021-02-15
    相关资源
    最近更新 更多