Read JSON to pandas dataframe - ValueError: Mixing dicts with non-Series may lead to ambiguous ordering

后端 未结 3 1711
后悔当初
后悔当初 2021-02-12 14:50

I am trying to read in the JSON structure below into pandas dataframe, but it throws out the error message:

ValueError: Mixing dicts with non-Series may

相关标签:
3条回答
  • 2021-02-12 14:58

    If you need only the result part in the data frame, here is the code to help you:

    import json
    import pandas as pd
    data = json.load(open('json_file.json'))
    
    df = pd.DataFrame(data["result"])
    

    To the best of my knowledge, the ValueError occurs because the data types are all over the place, some strings, some lists, multiple {} etc. This error may be solved by normalizing the data. To do that, here is the code below:

    import json
    
    with open('json_file.json') as project_file:    
        data = json.load(project_file)  
    
    df = pd.json_normalize(data)
    
    0 讨论(0)
  • 2021-02-12 15:14

    You can use json_normalize with assign:

    from pandas.io.json import json_normalize
    import json
    
    with open('json_file.json') as data_file:    
        d= json.load(data_file)  
    
    df = json_normalize(d, 'result').assign(**d['status'])
    print (df)
       club_id  id  statuscode  statusmessage
    0    16182  22         200  Everything OK
    1    16182  23         200  Everything OK
    2    16182  24         200  Everything OK
    3    16182  25         200  Everything OK
    4    16182  26         200  Everything OK
    5    16182  27         200  Everything OK
    
    0 讨论(0)
  • 2021-02-12 15:25

    If you just need the result part in a dataframe, then here is the code to help you.

    import json
    import pandas as pd
    data = json.load(open('json_file.json'))
    
    df = pd.DataFrame(data["result"])
    
    0 讨论(0)
提交回复
热议问题