我想要完成的目标是对“mjtheme_namecode”列中出现的任何数字的次数执行“计数”。我的导师说我们必须使用json_normalize方法来完成这项任务。
通常我会使用pd.value_counts(),但单元格中还有其他文本,所以这是不可能的。
格式如下:
我已经使用pd.read_json将数据读入pandas数据帧,然后在列mjtheme_namecode上进行过滤。我已尝试使用json_normalize访问数据,如下所示,但我继续得到一个错误说明:“字符串索引必须是整数”。
sample = df.to_json #Convert series to a json string
json_normalize(sample2, 'name')
有人能指出我如何解决这个问题吗?
谢谢!
答案 0 :(得分:1)
对我来说工作:
import json
from pandas.io.json import json_normalize
with open('world_bank_projects.json') as data_file:
data = json.load(data_file)
df = json_normalize(data, 'mjtheme_namecode')
print (df.head())
code name
0 8 Human development
1 11
2 1 Economic management
3 6 Social protection and risk management
4 5 Trade and integration