【问题标题】:Create Pandas dataframe from dictionary - python3从字典创建 Pandas 数据框 - python3
【发布时间】:2020-04-03 02:56:50
【问题描述】:

我想把这个数据结构转换成pandas数据框:

data = json.dumps(brasil[0]["timelines"]["confirmed"]["timeline"], indent=2)

哪个打印:

{
  "2020-01-22T00:00:00Z": 0,
  "2020-01-23T00:00:00Z": 0,
  "2020-01-24T00:00:00Z": 0,
  "2020-01-25T00:00:00Z": 0,
  "2020-01-26T00:00:00Z": 0,
  "2020-01-27T00:00:00Z": 0,
  "2020-01-28T00:00:00Z": 0,
  "2020-01-29T00:00:00Z": 0,
  "2020-01-30T00:00:00Z": 0,
  "2020-01-31T00:00:00Z": 0,
  "2020-02-01T00:00:00Z": 0,
  "2020-02-02T00:00:00Z": 0,
  "2020-02-03T00:00:00Z": 0,
  "2020-02-04T00:00:00Z": 0,
  "2020-02-05T00:00:00Z": 0,
  "2020-02-06T00:00:00Z": 0,
  "2020-02-07T00:00:00Z": 0,
  "2020-02-08T00:00:00Z": 0,
  "2020-02-09T00:00:00Z": 0,
  "2020-02-10T00:00:00Z": 0,
  "2020-02-11T00:00:00Z": 0,
  "2020-02-12T00:00:00Z": 0,
  "2020-02-13T00:00:00Z": 0,
  "2020-02-14T00:00:00Z": 0,
  "2020-02-15T00:00:00Z": 0,
  "2020-02-16T00:00:00Z": 0,
  "2020-02-17T00:00:00Z": 0,
  "2020-02-18T00:00:00Z": 0,
  "2020-02-19T00:00:00Z": 0,
  "2020-02-20T00:00:00Z": 0,
  "2020-02-21T00:00:00Z": 0,
  "2020-02-22T00:00:00Z": 0,
  "2020-02-23T00:00:00Z": 0,
  "2020-02-24T00:00:00Z": 0,
  "2020-02-25T00:00:00Z": 0,
  "2020-02-26T00:00:00Z": 1,
  "2020-02-27T00:00:00Z": 1,
  "2020-02-28T00:00:00Z": 1,
  "2020-02-29T00:00:00Z": 2,
  "2020-03-01T00:00:00Z": 2,
  "2020-03-02T00:00:00Z": 2,
  "2020-03-03T00:00:00Z": 2,
  "2020-03-04T00:00:00Z": 4,
  "2020-03-05T00:00:00Z": 4,
  "2020-03-06T00:00:00Z": 13,
  "2020-03-07T00:00:00Z": 13,
  "2020-03-08T00:00:00Z": 20,
  "2020-03-09T00:00:00Z": 25,
  "2020-03-10T00:00:00Z": 31,
  "2020-03-11T00:00:00Z": 38,
  "2020-03-12T00:00:00Z": 52,
  "2020-03-13T00:00:00Z": 151,
  "2020-03-14T00:00:00Z": 151,
  "2020-03-15T00:00:00Z": 162,
  "2020-03-16T00:00:00Z": 200,
  "2020-03-17T00:00:00Z": 321,
  "2020-03-18T00:00:00Z": 372,
  "2020-03-19T00:00:00Z": 621,
  "2020-03-20T00:00:00Z": 793,
  "2020-03-21T00:00:00Z": 1021,
  "2020-03-22T00:00:00Z": 1546,
  "2020-03-23T00:00:00Z": 1924,
  "2020-03-24T00:00:00Z": 2247,
  "2020-03-25T00:00:00Z": 2554,
  "2020-03-26T00:00:00Z": 2985,
  "2020-03-27T00:00:00Z": 3417,
  "2020-03-28T00:00:00Z": 3904,
  "2020-03-29T00:00:00Z": 4256,
  "2020-03-30T00:00:00Z": 4579,
  "2020-03-31T00:00:00Z": 5717,
  "2020-04-01T00:00:00Z": 6836,
  "2020-04-02T00:00:00Z": 8044
}

我试过了:

df = pd.DataFrame([data.items()], columns=['date', 'deaths'])

但我得到了错误:

AttributeError: 'str' object has no attribute 'items'

我该怎么做?

【问题讨论】:

  • 您确定data 是字典吗?从错误来看,好像是一个字符串。
  • 当我打印(数据)时,它会像上面一样打印字典
  • type(data) 返回什么?
  • ...
  • 我相信 json.dump(...) 会给你一个 JSON 字符串。所以data是一个字典类型的JSON字符串。你可以偷懒做pd.DataFrame(eval(data))。或者你可以手动将字符串data解析为dict。

标签: python-3.x pandas dictionary


【解决方案1】:

试试:

df = pd.DataFrame.from_dict(data, orient='index')

重命名:

df = pd.DataFrame.from_dict(data, orient='index')\
       .rename_axis('date')\
       .rename(columns={0:'deaths'}).reset_index()

输出:

                    date  deaths
0   2020-01-22T00:00:00Z       0
1   2020-01-23T00:00:00Z       0
2   2020-01-24T00:00:00Z       0
3   2020-01-25T00:00:00Z       0
4   2020-01-26T00:00:00Z       0
..                   ...     ...
67  2020-03-29T00:00:00Z    4256
68  2020-03-30T00:00:00Z    4579
69  2020-03-31T00:00:00Z    5717
70  2020-04-01T00:00:00Z    6836
71  2020-04-02T00:00:00Z    8044

[72 rows x 2 columns]

【讨论】:

  • 我收到if isinstance(list(data.values())[0], (Series, dict)): AttributeError: 'str' object has no attribute 'values'
猜你喜欢
  • 2017-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 2022-09-28
  • 2016-10-22
  • 1970-01-01
  • 2018-07-14
相关资源
最近更新 更多