Python 如何基于JSON文件中的另一个值使用JSON计算值
我有一个JSON文件,由一个包含字典的数组组成,每个字典都是买家对特定车库的意见。 我想知道在每个车库中,每种车型出现的次数,如下所示:Python 如何基于JSON文件中的另一个值使用JSON计算值,python,json,dictionary,Python,Json,Dictionary,我有一个JSON文件,由一个包含字典的数组组成,每个字典都是买家对特定车库的意见。 我想知道在每个车库中,每种车型出现的次数,如下所示: [ {"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": "
[
{"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。
我尝试使用reduce()计算发生次数,但失败
输出示例:
{“车库”:“mike_gar”,“类型”:{“ford”:2,“mazda”:1}}
这是一个基于简化的解决方案。首先,我测试累积字典中是否存在车库,如果不存在,则创建车库。然后,我检查车库字典中是否存在汽车类型,如果不存在,我就创建它。最后,我增加了汽车类型
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
输出:
{
“mike_-gar”:{“garage”:“mike_-gar”,“types”:{“ford”:2,“mazda”:1},
“bill_gar”:{“garage”:“bill_gar”,“types”:{“kia”:1},
“alison_gar”:{“garage”:“alison_gar”,“types”:{“起亚”:1,“马自达”:1}}
}
如果您希望结果为数组,请使用
res.values()
您只需解析数据并按以下方式进行计数:
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
执行上述操作的输出为:
[{'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}}]
Pandas软件包非常适合处理此类数据。您可以轻松地将列表转换为数据帧
import pandas as pd
df = pd.DataFrame(g_arr)
print(df)
印刷品:
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
garage car_type
alison_gar kia 1
mazda 1
bill_gar kia 1
mike_gar ford 2
mazda 1
dtype: int64
然后,您可以使用.groupby()
方法对数据进行分组,并使用.size()
方法获取每组的行数
print(df.groupby(['garage', 'car_type']).size())
印刷品:
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
garage car_type
alison_gar kia 1
mazda 1
bill_gar kia 1
mike_gar ford 2
mazda 1
dtype: int64
杰森是车库的主人吗?@wim我想应该是JSON。修好了。@wim oppsss…@Shelly875我想你会觉得很有帮助的。让我们知道这是否解决了您的问题。这不是一个完全相同的版本,但它的关系非常密切。这是一个Javascript解决方案,这个问题有一个Python标记。我已经尝试了代码,这正是我想要的!谢谢:)