pandas read_json: “If using all scalar values, you must pass an index”

后端 未结 3 890
时光说笑
时光说笑 2020-12-09 15:56

I have some difficulty in importing a JSON file with pandas.

import pandas as pd
map_index_to_word = pd.read_json(\'people_wiki_map_index_to_word.json\')


        
相关标签:
3条回答
  • 2020-12-09 16:01

    You can do as @ayhan mention which will give you a column base format

    Or you can enclose the object in [ ] (source) as shown below to give you a row format that will be convenient if you are loading multiple values and planing on using matrix for your machine learning models.

    df = pd.DataFrame([data])
    

    0 讨论(0)
  • 2020-12-09 16:21

    I think what is happening is that the data in

    map_index_to_word = pd.read_json('people_wiki_map_index_to_word.json')
    

    is being read as a string instead of a json

    {"biennials": 522004, "lb915": 116290, "shatzky": 127647, "woode": 174106, "damfunk": 133206, "nualart": 153444, "hatefillot": 164111, "missionborn": 261765, "yeardescribed": 161075, "theoryhe": 521685}
    

    is actually

    '{"biennials": 522004, "lb915": 116290, "shatzky": 127647, "woode": 174106, "damfunk": 133206, "nualart": 153444, "hatefillot": 164111, "missionborn": 261765, "yeardescribed": 161075, "theoryhe": 521685}'
    

    Since a string is a scalar, it wants you to load it as a json, you have to convert it to a dict which is exactly what the other response is doing

    The best way is to do a json loads on the string to convert it to a dict and load it into pandas

    myfile=f.read()
    jsonData=json.loads(myfile)
    df=pd.DataFrame(data)
    
    0 讨论(0)
  • 2020-12-09 16:23

    Try

    ser = pd.read_json('people_wiki_map_index_to_word.json', typ='series')
    

    That file only contains key value pairs where values are scalars. You can convert it to a dataframe with ser.to_frame('count').

    You can also do something like this:

    import json
    with open('people_wiki_map_index_to_word.json', 'r') as f:
        data = json.load(f)
    

    Now data is a dictionary. You can pass it to a dataframe constructor like this:

    df = pd.DataFrame({'count': data})
    
    0 讨论(0)
提交回复
热议问题