繁体   English   中英

使用 Pandas 读取嵌套 json 中的计数值

[英]Counting values in nested json read using pandas

我想在规范化嵌套列后计算主题的数量。

这是我的数据示例:

0    [{'code': '8', 'name': 'Human development'}, {'code': '11', 'name': ''}]                                                                                                                                                                   
1    [{'code': '1', 'name': 'Economic management'}, {'code': '6', 'name': 'Social protection and risk management'}]                                                                                                                             
2    [{'code': '5', 'name': 'Trade and integration'}, {'code': '2', 'name': 'Public sector governance'}, {'code': '11', 'name': 'Environment and natural resources management'}, {'code': '6', 'name': 'Social protection and risk management'}]
3    [{'code': '7', 'name': 'Social dev/gender/inclusion'}, {'code': '7', 'name': 'Social dev/gender/inclusion'}]                                                                                                                               
4    [{'code': '5', 'name': 'Trade and integration'}, {'code': '4', 'name': 'Financial and private sector development'}]                                                                                                                        
Name: mjtheme_namecode, dtype: object

这是我尝试过的:

from pandas.io.json import json_normalize
result = json_normalize(json_file, 'mjtheme_namecode').name.value_counts()

但是这会返回错误

TypeError: string indices must be integers

我认为问题在于您读取 json 文件的方式, mjtheme_namecode 应该是一个长列表,而不是列表或类似的列表。 尝试将 max_level=0。 另一种可能性是空字段的问题。 尝试输入默认值(请参阅: Pandas json_normalize 和 JSON 中的 null 值

我设法得到这样的结果:

from pandas.io.json import json_normalize

mjtheme_namecode =[{'code':'8','name':'Humandevelopment'},{'code':'11','name':''},{'code':'1','name':'Economicmanagement'},{'code':'6','name':'Socialprotectionandriskmanagement'},
                   {'code':'5','name':'Tradeandintegration'},{'code':'2','name':'Publicsectorgovernance'},{'code':'11','name':'Environmentandnaturalresourcesmanagement'},{'code':'6','name':'Socialprotectionandriskmanagement'},
                   {'code':'7','name':'Socialdev/gender/inclusion'},{'code':'7','name':'Socialdev/gender/inclusion'},
                   {'code':'5','name':'Tradeandintegration'},{'code':'4','name':'Financialandprivatesectordevelopment'}]
print(mjtheme_namecode)

result = json_normalize(mjtheme_namecode).name.value_counts()
print(result)
Socialdev/gender/inclusion                  2
Socialprotectionandriskmanagement           2
Tradeandintegration                         2
Humandevelopment                            1
Publicsectorgovernance                      1
Environmentandnaturalresourcesmanagement    1
Financialandprivatesectordevelopment        1
Economicmanagement                          1
                                            1
Name: name, dtype: int64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM