首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据JSON文件中的另一个值计算JSON值

如何根据JSON文件中的另一个值计算JSON值
EN

Stack Overflow用户
提问于 2018-12-31 21:26:58
回答 3查看 85关注 0票数 1

我有一个由一个包含字典的数组组成的JSON文件,每个字典都是一个特定车库的买家的意见。我想知道我在每个车库里每辆车的类型发生了多少次,看起来如下:

代码语言:javascript
复制
[
  {"garage": "mike_gar", "reliability": 6, "car_type": "ford", "time": "16:10:36"},
  {"garage": "bill_gar", "reliability": 5,"car_type": "kia", "time": "4:37:22"},
  {"garage": "alison_gar", "reliability": 1, "car_type": "kia", "time": "11:25:40"},
  {"garage": "alison_gar", "reliability": 10, "car_type": "mazda", "time": "2:18:42"},
  {"garage": "mike_gar", "reliability": 3, "car_type": "mazda", "time": "12:14:20"},
  {"garage": "mike_gar", "reliability": 2, "car_type": "ford", "time": "2:08:27"}
]

假设我们已经从JSON文件读取到变量g_arr。我试着用减缩()来计数发生的情况,但没有成功。

输出示例: {"garage" : "mike_gar", "types":{"ford" : 2, "mazda": 1}}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-31 21:51:49

这里有一个基于缩减的解决方案。首先,我在累积字典中测试车库是否存在,如果没有,创建它。然后,我检查车库字典中是否存在汽车类型,如果不存在,我就创建它。最后,我增加了汽车类型。

代码语言:javascript
复制
res = {}

for d in garages:
    if d["garage"] not in res:
        res[d["garage"]] = {"garage": d["garage"], "types": {}}

    if d["car_type"] not in res[d["garage"]]["types"]:
        res[d["garage"]]["types"][d["car_type"]] = 0

    res[d["garage"]]["types"][d["car_type"]] += 1

输出:

代码语言:javascript
复制
{
  'mike_gar': {'garage': 'mike_gar', 'types': {'ford': 2, 'mazda': 1}},
  'bill_gar': {'garage': 'bill_gar', 'types': {'kia': 1}}, 
  'alison_gar': {'garage': 'alison_gar', 'types': {'kia': 1, 'mazda': 1}}
}

试试看!

如果希望在数组中获得结果,请使用res.values()

票数 1
EN

Stack Overflow用户

发布于 2018-12-31 21:57:00

您可以简单地解析数据并按以下方式进行计数:

代码语言:javascript
复制
garages = []
cars = []
output = []

for element in data:
    if element['garage'] not in garages: garages.append(element['garage'])
    if element['car_type'] not in cars: cars.append(element['car_type'])

for type in garages:
    current = {}
    current['types'] = {}
    current['garage'] = type
    for element in data:
        if element['car_type'] not in current['types']:
            current['types'][element['car_type']]=0

        if current['garage'] == element['garage']:
            for car_type in cars:
                if element['car_type'] == car_type:
                    current['types'][element['car_type']]+=1
    output.append(current)

print output

执行上述操作的输出是:

代码语言:javascript
复制
[{'garage': 'mike_gar', 'types': {'mazda': 1, 'kia': 0, 'ford': 2}}, {'garage': 'bill_gar', 'types': {'mazda': 0, 'kia': 1, 'ford': 0}}, {'garage': 'alison_gar', 'types': {'mazda': 1, 'kia': 1, 'ford': 0}}]
票数 1
EN

Stack Overflow用户

发布于 2019-01-01 14:17:57

熊猫套装对于处理这样的数据是很好的。您可以轻松地将您的列表转换为Pandas数据。

代码语言:javascript
复制
import pandas as pd

df = pd.DataFrame(g_arr)
print(df)

指纹:

代码语言:javascript
复制
  car_type      garage  reliability      time
0     ford    mike_gar            6  16:10:36
1      kia    bill_gar            5   4:37:22
2      kia  alison_gar            1  11:25:40
3    mazda  alison_gar           10   2:18:42
4    mazda    mike_gar            3  12:14:20
5     ford    mike_gar            2   2:08:27

无法使用.groupby()方法对数据进行分组,而使用.size()方法获取每个组的行数。

代码语言:javascript
复制
print(df.groupby(['garage', 'car_type']).size())

指纹:

代码语言:javascript
复制
garage      car_type
alison_gar  kia         1
            mazda       1
bill_gar    kia         1
mike_gar    ford        2
            mazda       1
dtype: int64
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53991537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档