【问题标题】:Convert Dictionary to Python Dataframe [duplicate]将字典转换为 Python 数据框 [重复]
【发布时间】:2019-10-19 23:43:54
【问题描述】:
Dict = {'type': 'book', 'text_eng': 'National accounts', 'text': 'Volkswirtschaftliche Gesamtrechnungen', 'anchor': 'GESAMTVOLK'}
pd.DataFrame(Dict)
pd.DataFrame.from_dict(Dict)

上面的脚本抛出错误:“ValueError: If using all scalar values, you must pass an index”

我需要字典键作为数据框列和行中的字典值。

以下脚本可以正常工作,因为字典值添加为列表

Dict = {'type': ['book'], 'text_eng': ['National accounts'], 'text': ['Volkswirtschaftliche Gesamtrechnungen'], 'anchor': ['GESAMTVOLK']}

请告诉我如何做到这一点?

【问题讨论】:

标签: python python-3.x pandas dataframe


【解决方案1】:

用途:

import pandas as pd

D = {'type': 'book', 'text_eng': 'National accounts', 'text': 'Volkswirtschaftliche Gesamtrechnungen', 'anchor': 'GESAMTVOLK'}
a = pd.DataFrame([D])
print(a)

输出:

       anchor                                   text           text_eng  type
0  GESAMTVOLK  Volkswirtschaftliche Gesamtrechnungen  National accounts  book

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2017-04-19
    • 2018-09-12
    相关资源
    最近更新 更多