简体   繁体   中英

Convert JSON (key is column, value is record) to dataframe

I have a JSON/dictionary object that contains data pulled from a web service.

import pandas as pd
import json

json1 = json.loads(data_from_webservice)
print(json1)
Out[1]: 
{'data': 
  [
    {'Region': 'West', 'Airport': 'LAX', 'Score': 3, 'index': 0},
    {'Region': 'West', 'Airport': 'SFO', 'Score': 6, 'index': 1},
    {'Region': 'East', 'Airport': 'YYZ', 'Score': 9, 'index': 2}
  ]
}

How do I read this into a dataframe such that the keys are columns and values are the records. Also the index of the dataframe uses the index key like so:

  Region Airport  Score
0   West     LAX      3
1   West     SFO      6
2   East     YYZ      9

Using the from_dict() method does not help:

df2 = pd.DataFrame.from_dict(json3, orient='columns')
print(df2)
Out[2]: 
                                                data
0  {'Region': 'West', 'Airport': 'LAX', 'Score': ...
1  {'Region': 'West', 'Airport': 'SFO', 'Score': ...
2  {'Region': 'East', 'Airport': 'YYZ', 'Score': ...

Simple solution

a={'data': 
  [
    {'Region': 'West', 'Airport': 'LAX', 'Score': 3, 'index': 0},
    {'Region': 'West', 'Airport': 'SFO', 'Score': 6, 'index': 1},
    {'Region': 'East', 'Airport': 'YYZ', 'Score': 9, 'index': 2}
  ]
}
pd.DataFrame(a['data'])

Also you can read JSON data directly

pd.read_json(your_json,orient='split')

You can also use the built-in json_normalize in pandas .

pd.io.json.json_normalize(json1, 'data')

  Airport Region  Score  index
0     LAX   West      3      0
1     SFO   West      6      1
2     YYZ   East      9      2 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM