本文介绍了pandas read_json:“如果使用所有标量值,则必须传递索引"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在导入带有熊猫的JSON文件时遇到一些困难.
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')
这是我得到的错误:
ValueError: If using all scalar values, you must pass an index
文件结构是这样简化的:
The file structure is simplified like this:
{"biennials": 522004, "lb915": 116290, "shatzky": 127647, "woode": 174106, "damfunk": 133206, "nualart": 153444, "hatefillot": 164111, "missionborn": 261765, "yeardescribed": 161075, "theoryhe": 521685}
它来自华盛顿大学Coursera的机器学习课程.您可以找到文件此处.
It is from the machine learning course of University of Washington on Coursera. You can find the file here.
推荐答案
尝试
ser = pd.read_json('people_wiki_map_index_to_word.json', typ='series')
该文件仅包含值是标量的键值对.您可以使用ser.to_frame('count')
将其转换为数据框.
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})
这篇关于pandas read_json:“如果使用所有标量值,则必须传递索引"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!