简体   繁体   中英

How do I extract the dictionary from a JSON file to a pandas Dataframe?

I've extracted the data into a JSON file (it is a nest filed)

I've read_json to extract the data into a pandas:

df = pd.read_json('./data0000.json')

but got this as the data is nested ( all the fields are under Data):

                             Data
0   {field 1, field 2, field 3....)      

How do I unnest it in a panda dataframe?

For the following data format, read_json() works as you would like:

    [
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
    ]

We would still need to see your data for a full fix, but you may also want to explore the "orient" parameter in read_json().

    data = pd.read_json('data0000.json',orient='records')

Allowed values for orient are: {'split','records','index','columns','values'}

From the docs link :

  • DataFrame

    • default is 'columns'
    • allowed values are: {'split','records','index','columns','values'}
    • The DataFrame index must be unique for orients 'index' and 'columns'.
    • The DataFrame columns must be unique for orients 'index', 'columns', and 'records'.

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