【问题标题】:How to convert CSV to nested JSON(upto level 3) in Python如何在 Python 中将 CSV 转换为嵌套 JSON(最高级别 3)
【发布时间】:2016-09-23 23:07:36
【问题描述】:

我有以下 CSV 格式的数据。

id,category,sub_category,sub_category_type,count
0,fruits,citrus,lemon,30
1,fruits,citrus,lemon,40
2,fruits,citrus,lemon,50
3,fruits,citrus,grapefruit,20
4,fruits,citrus,orange,40
5,fruits,citrus,orange,10
6,fruits,berries,blueberry,20
7,fruits,berries,strawberry,50
8,fruits,berries,strawberry,90
9,fruits,berries,cranberry,70
10,fruits,berries,raspberry,16
11,fruits,berries,raspberry,80
12,fruits,dried fruit,raisins,10
13,fruits,dried fruit,dates,15
14,fruits,dried fruit,dates,10
15,vegetables,legumes,beans,12
16,vegetables,legumes,beans,15
17,vegetables,legumes,chickpea,12
18,vegetables,green leaf,spinach,18
19,vegetables,green leaf,cress,19

我想将上述 CSV 格式转换为嵌套 JSON,因为 pandas.DataFrame.to_json() 无法帮助我转换为嵌套 JSON 格式。

有什么解决办法吗?

PS:我以问答方式回答上述问题以分享知识。我很高兴知道是否有比这更好的解决方案。

【问题讨论】:

  • 为什么你一问就马上回答。您的回答应该是您最初问题的一部分。
  • 那你能解释一下我应该如何回答这个问题吗?当我单击复选框回答您自己的问题 - 分享您的知识问答风格时,它会打开一个文本框来发布答案。如果我的回答应该是我的问题的一部分,那么为什么必须有另一个文本框来发布答案?
  • 或者,您的回答可能是您已经尝试过的东西(这应该是问题的一部分),但无论出于何种原因,您都在寻求更好的解决方案。

标签: python json csv pandas


【解决方案1】:

以下代码的灵感来自this github 链接。此代码将帮助我们将 CSV 转换为 3 级嵌套 JSON

import pandas as pd
import json


df = pd.read_csv('data.csv')

# choose columns to keep, in the desired nested json hierarchical order
df = df[["category", "sub_category","sub_category_type", "count"]]

# order in the groupby here matters, it determines the json nesting
# the groupby call makes a pandas series by grouping "category", "sub_category" and"sub_category_type", 
#while summing the numerical column 'count'
df1 = df.groupby(["category", "sub_category","sub_category_type"])['count'].sum()
df1 = df1.reset_index()

print df1

d = dict()
d = {"name":"stock", "children": []}

for line in df1.values:
    category = line[0]
    sub_category = line[1]
    sub_category_type = line[2]
    count = line[3]

    # make a list of keys
    category_list = []
    for item in d['children']:
        category_list.append(item['name'])

    # if 'category' is NOT category_list, append it
    if not category in category_list:
        d['children'].append({"name":category, "children":[{"name":sub_category, "children":[{"name": sub_category_type, "count" : count}]}]})

    # if 'category' IS in category_list, add a new child to it
    else:
        sub_list = []        
        for item in d['children'][category_list.index(category)]['children']:
            sub_list.append(item['name'])
        print sub_list

        if not sub_category in sub_list:
            d['children'][category_list.index(category)]['children'].append({"name":sub_category, "children":[{"name": sub_category_type, "count" : count}]})
        else:
            d['children'][category_list.index(category)]['children'][sub_list.index(sub_category)]['children'].append({"name": sub_category_type, "count" : count})


print json.dumps(d)

在执行时,

{
"name": "stock", 
"children": [
    {"name": "fruits",
    "children": [
        {"name": "berries", 
        "children": [
            {"count": 20, "name": "blueberry"}, 
            {"count": 70, "name": "cranberry"}, 
            {"count": 96, "name": "raspberry"}, 
            {"count": 140, "name": "strawberry"}]
        },
        {"name": "citrus", 
        "children": [
            {"count": 20, "name": "grapefruit"},
            {"count": 120, "name": "lemon"},
            {"count": 50, "name": "orange"}]
        }, 
        {"name": "dried fruit",
        "children": [
            {"count": 25, "name": "dates"}, 
            {"count": 10, "name": "raisins"}]
        }]
    },
    {"name": "vegtables",
    "children": [
        {"name": "green leaf",
        "children": [
            {"count": 19, "name": "cress"},
            {"count": 18, "name": "spinach"}]
        },
        {
        "name": "legumes",
        "children": [
            {"count": 27, "name": "beans"},
            {"count": 12, "name": "chickpea"}]
        }]
    }]
}

【讨论】:

    猜你喜欢
    • 2022-08-21
    • 1970-01-01
    • 2018-10-27
    • 2021-05-17
    • 2019-07-21
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多