[英]Python: json normalize “String indices must be integers” error
I am getting a type error as "TypeError: string indices must be integers" in the following code. 在以下代码中,我收到“ TypeError:字符串索引必须为整数”的类型错误。
import pandas as pd
import json
from pandas.io.json import json_normalize
full_json_df = pd.read_json('data/world_bank_projects.json')
json_nor = json_normalize(full_json_df, 'mjtheme_namecode')
json_nor.groupby('name')['code'].count().sort_values(ascending=False).head(10)
Output:
TypeError
Traceback (most recent call last)
<ipython-input-28-9401e8bf5427> in <module>()
1 # Find the top 10 major project themes (using column 'mjtheme_namecode')
2
----> 3 json_nor = json_normalize(full_json_df, 'mjtheme_namecode')
4 #json_nor.groupby('name')['code'].count().sort_values(ascending = False).head(10)
TypeError: string indices must be integers
According to pandas documentation , for data
argument of the method json_normalize
: 根据pandas文档 ,对于
json_normalize
方法的data
参数:
data : dict or list of dicts Unserialized JSON objects
data:字典或字典列表未序列化的JSON对象
In above, pd.read_json
returns dataframe
. 另外,在上述,
pd.read_json
返回dataframe
。 So, you can try converting dataframe
to dictionary
using .to_dict()
. 因此,您可以尝试使用
.to_dict()
将dataframe
转换为dictionary
。 There are various options for using to_dict() as well. 也有多种使用to_dict()的选项。
May be something like below: 可能如下所示:
json_normalize(full_json_df.to_dict(), ......)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.