Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

python - Group By and Count occurences of values in list of nested dicts

I have a JSON file that looks structurally like this:

{
  "content": [
    {
      "name": "New York",
      "id": "1234",
      "Tags": {
        "hierarchy": "CITY"
      }
    },
    {
      "name": "Los Angeles",
      "id": "1234",
      "Tags": {
        "hierarchy": "CITY"
      }
    },
    {
      "name": "California",
      "id": "1234",
      "Tags": {
        "hierarchy": "STATE"
      }
    }
  ]
}

And as an outcome I would like a table view in CSV like so:

tag.key tag.value occurrance
hierarchy CITY 2
hierarchy STATE 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly construct a dictionary object by using ast.literal_eval function, and then split this object to get a key, value tuples in order to create a dataframe by using zip. Apply groupby to newly formed dataframe, and finally create a .csv file through use of df_agg.to_csv such as

import json
import ast
import pandas as pd    
Js= """{
  "content": [
    {
      "name": "New York",
      "id": "1234",
      "Tags": {
        "hierarchy": "CITY"
      }
    },
    ....
    ....
    {
      "name": "California",
      "id": "1234",
      "Tags": {
        "hierarchy": "STATE"
      }
    }
  ]
}"""
data = ast.literal_eval(Js)     
key = []
value=[]
for i in list(range(0,len(data['content']))):
    value.append(data['content'][i]['Tags']['hierarchy'])
    for j in data['content'][i]['Tags']:
        key.append(j)

df = pd.DataFrame(list(zip(key, value)), columns =['tag.key', 'tag.value'])
df_agg=df.groupby(['tag.key', 'tag.value']).size().reset_index(name='occurrance')
df_agg.to_csv(r'ThePath\to\your\file\result.csv',index = False)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...