Json进入数据帧大熊猫

时间:2017-06-02 21:36:06

标签: python json python-3.x pandas dataframe

我在json下面,我从一些网址获得:

{
    "abc":
      {
        "123":[45600,null,3567],
        "378":[78689,2345,5678],
        "343":[23456,null,null]
      }
}

它存储在json_obj对象中:

json_obj = response.json()

我需要将这个json转换为数据帧,我的代码应该是这样的:

df = pd.read_json(response,orient ='columns')

所以,结果应该是:

          abc
123      [45600,null,3567]
378      [78689,2345,5678]
343      [23456,null,null]

但是上面的代码我得到错误:

date_unit).parse()
  self._parse_no_numpy()
  loads(json, precise_float=self.precise_float), dtype=None)
  TypeError: Expected String or Unicode

如果我在上面的代码中用url替换响应。它会工作正常。但是,我需要传递json_object而不是url。

请提出建议。

1 个答案:

答案 0 :(得分:1)

df = pd.read_json(response,orient='columns')

read_json()接受JSON数据。 "响应",我相信会为您正在制作的某些请求存储API响应。

response.json()会给你响应主体的python字典。 您需要将其转换为JSON。 试试这个:

import json
df = pd.read_json(json.dumps(response.json()),orient='columns')