【问题标题】:Convert nested JSON to pandas DataFrame将嵌套 JSON 转换为 pandas DataFrame
【发布时间】:2019-06-09 22:16:28
【问题描述】:

我有一个嵌套的 JSON,我想提取其中的一部分并将其制成 pandas DataFrame。我浏览的任何 stackoverflow 帖子都无法正常工作!

我尝试了使用现有帖子的不同方法,但无法正常工作

来自“结果”的 JSON

 b'{"coin":{"id":363,"name":"Bitcoin","code":"BTC"},"dataType":"marketCap","baseCurrency":"USD","data":[{"date":"2018-01-12","marketCap":"232547809668.32000000"},{"date":"2018-01-13","marketCap":"241311607656.32000000"}

代码:

http = urllib3.PoolManager()
url = 'https://www.cryptocurrencychart.com/api/coin/history/363/2018-01-12/2019-01-12/marketCap/USD'
headers = urllib3.util.make_headers(basic_auth='xxx:xxx')
r = http.request('GET', url , headers = headers) 
result = r.data

df = json_normalize(result['data'])
df.set_index('date', inplace = True)

结果是类型:字节

错误:

TypeError: byte indices must be integers or slices, not str

预期的数据帧

            BTCmarketCap
2019-01-01  xxxxxxx 
2019-01-02  xxxxxx

【问题讨论】:

    标签: json python-3.x pandas


    【解决方案1】:

    要解压字典,请使用 json_normalizerecord_path=... 参数。

    import pandas.io.json as pd_json
    
    data = pd_json.loads(result)
    pd_json.json_normalize(data, record_path='data')
    
             date              marketCap
    0  2018-01-12  232547809668.32000000
    1  2018-01-13  241311607656.32000000
    

    如果您还想要其他值,请传递 meta=.... 参数:

    df = pd_json.json_normalize(data, 
                                record_path='data', 
                                meta=['coin', 'dataType', 'baseCurrency'])
    df
    
             date              marketCap     ...        dataType baseCurrency
    0  2018-01-12  232547809668.32000000     ...       marketCap          USD
    1  2018-01-13  241311607656.32000000     ...       marketCap          USD
    
     df.columns
    # Index(['date', 'marketCap', 'coin', 'dataType', 'baseCurrency'], dtype='object')
    

    【讨论】:

    • 谢谢!嗯,我在上面遇到了这个错误: AttributeError: 'HTTPResponse' object has no attribute 'json'
    • @AlexanderThomsen 对不起,假设是请求,没有看清楚。进行了修改,现在检查吗?
    • 感谢上帝!所以为了直截了当,您加载 JSON 并对其进行解码(无论做什么),同时将其制成数据帧,然后对其进行规范化并仅选择“数据”路径。
    • @AlexanderThomsen 是一个字节串。实际上,不需要解码,因为loads 无论如何都会这样做。只需传递字符串,将其转换为字典,然后使用json_normalize 通过选择要提取的适当记录路径对其进行解码。
    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 2021-03-07
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2021-12-29
    • 2013-11-16
    相关资源
    最近更新 更多