Convert JSON API response to pandas Dataframe

前端 未结 1 1597
误落风尘
误落风尘 2020-12-15 11:14

I\'m struggling to convert a JSON API response into a pandas Dataframe object. I\'ve read answers to similar questions/documentation but nothing has helped. My closest attem

相关标签:
1条回答
  • 2020-12-15 11:40

    I think you need json_normalize:

    from pandas import json_normalize 
    
    df = json_normalize(d, 'abc')
    print (df)
            amount     price      tid   timestamp type
    0  2321.379525  0.000062  8577050  1498649162  bid
    1   498.789936  0.000062  8577047  1498649151  bid
    

    For multiple keys is possible use concat with list comprehension and DataFrame constructor:

    d =  {'abc': [{'type': 'bid', 'price': 6.194e-05, 'amount': 2321.37952545, 'tid': 8577050, 'timestamp': 1498649162}, {'type': 'bid', 'price': 6.194e-05, 'amount': 498.78993587, 'tid': 8577047, 'timestamp': 1498649151}],
          'def': [{'type': 'bid', 'price': 6.194e-05, 'amount': 2321.37952545, 'tid': 8577050, 'timestamp': 1498649162}, {'type': 'bid', 'price': 6.194e-05, 'amount': 498.78993587, 'tid': 8577047, 'timestamp': 1498649151}]}
    

    df = pd.concat([pd.DataFrame(v) for k,v in d.items()], keys=d)
    print (df)
                amount     price      tid   timestamp type
    abc 0  2321.379525  0.000062  8577050  1498649162  bid
        1   498.789936  0.000062  8577047  1498649151  bid
    def 0  2321.379525  0.000062  8577050  1498649162  bid
        1   498.789936  0.000062  8577047  1498649151  bid
    
    0 讨论(0)
提交回复
热议问题