【问题标题】:How to write json to pandas DataFrame?如何将json写入pandas DataFrame?
【发布时间】:2020-06-12 20:31:39
【问题描述】:

试图弄清楚如何将 json 写入 pandas DataFrame。在我的代码中:

...
resp = s.get(URL)
df = pd.DataFrame.from_dict(resp.json())
print(df.to_string())

我在 json 中的响应如下所示:

{
    "instrument": "EUR_USD",
    "granularity": "S5",
    "candles": [
        {
            "complete": true,
            "volume": 8,
            "time": "2020-02-28T17:22:55.000000000Z",
            "mid": {
                "o": "1.10087",
                "h": "1.10088",
                "l": "1.10083",
                "c": "1.10083"
            }
        },
        {
            "complete": false,
            "volume": 7,
            "time": "2020-02-28T17:23:00.000000000Z",
            "mid": {
                "o": "1.10084",
                "h": "1.10084",
                "l": "1.10078",
                "c": "1.10078"
            }
        }
    ]
}

如果我打印 df,我会得到:

  instrument granularity                                                                                                                                               candles
0    EUR_USD          S5   {'complete': True, 'volume': 17, 'time': '2020-02-28T17:26:55.000000000Z', 'mid': {'o': '1.10022', 'h': '1.10023', 'l': '1.10014', 'c': '1.10014'}}
1    EUR_USD          S5  {'complete': False, 'volume': 19, 'time': '2020-02-28T17:27:00.000000000Z', 'mid': {'o': '1.10012', 'h': '1.10024', 'l': '1.10012', 'c': '1.10024'}}

如何操作将 json 写入 pandas df,以便我的 df 看起来像:

time                                open        high     low      close    volume
2020-02-28T17:26:55.000000000Z      1.10022     1.10023  1.10014  1.10014  17
...

【问题讨论】:

  • 你有没有做过研究,或者尝试过自己写一个解决方案?
  • 这能回答你的问题吗? JSON to pandas DataFrame
  • 是的,我做到了。你提问的目的是什么?
  • 那你能把它包含在你的帖子里吗?毕竟是相关信息。
  • 如果你向下滚动,已经有一个接受的答案。感谢您的意见。

标签: python json pandas


【解决方案1】:

您可以使用json_normalizepandas.io.json 下的一个方便的函数)从 DataFrame 中解析出剩余的嵌套 JSON 列。

from pandas.io.json import json_normalize

# Your current DataFrame
df
  instrument granularity                                            candles
0    EUR_USD          S5  {'complete': True, 'volume': 8, 'time': '2020-...
1    EUR_USD          S5  {'complete': False, 'volume': 7, 'time': '2020...

# Parse the `candles` column
parsed = json_normalize(df['candles'])
parsed
   complete  volume                            time    mid.o    mid.h    mid.l    mid.c
0      True       8  2020-02-28T17:22:55.000000000Z  1.10087  1.10088  1.10083  1.10083
1     False       7  2020-02-28T17:23:00.000000000Z  1.10084  1.10084  1.10078  1.10078

# Concat and rename columns as needed
renamer = {'mid.o': 'open', 
           'mid.h': 'high', 
           'mid.l': 'low', 
           'mid.c': 'close'}
res = pd.concat([df, parsed], axis=1)
res = res.drop(columns=['complete', 'candles']).rename(columns=renamer)
res
  instrument granularity  volume                            time     open     high      low    close
0    EUR_USD          S5       8  2020-02-28T17:22:55.000000000Z  1.10087  1.10088  1.10083  1.10083
1    EUR_USD          S5       7  2020-02-28T17:23:00.000000000Z  1.10084  1.10084  1.10078  1.10078

【讨论】:

    猜你喜欢
    • 2017-01-29
    • 1970-01-01
    • 2015-05-12
    • 2019-07-11
    • 1970-01-01
    • 2017-12-22
    • 2013-05-31
    • 2019-07-06
    相关资源
    最近更新 更多