首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python计算JSON系列中键的平均值

Python计算JSON系列中键的平均值
EN

Stack Overflow用户
提问于 2022-01-30 14:13:43
回答 1查看 60关注 0票数 0

我有一个pandas.core.series.Series,其中每个元素都是一个JSON,如下所示

代码语言:javascript
复制
0     {"count": 157065, "grp": {"a1": 12, "a2": 32}}
1     {"count": 2342, "grp": {"a1": 4, "a2": 34}}
2     {"count": 543, "grp": {"a1": 1, "a2": 11}}
3     {"count": 156, "grp": {"a1": 56, "a2": 75}}

如何计算所有JSON中count的平均值以及a1a2的平均值

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-30 14:30:50

我不完全确定这是不是你想要的。

这是为了计算“计数”的平均值。

代码语言:javascript
复制
doc1 = {"count": 157065, "grp": {"a1": 12, "a2": 32}}
doc2 = {"count": 2342, "grp": {"a1": 4, "a2": 34}}
doc3 = {"count": 543, "grp": {"a1": 1, "a2": 11}}
doc4 = {"count": 156, "grp": {"a1": 56, "a2": 75}}
lojs = [doc1, doc2, doc3, doc4] # list of all the jsons

countaverage = 0
# For every json, it gets the count and adds it to the variable I defined
for j in lojs:
    countaverage += j["count"]
# Divides it by the length of the amount of documents
countaverage = countaverage/len(lojs)

如果您想要获得a1的平均值,可以使用以下代码,或者使用上面的代码:

代码语言:javascript
复制
a1average = 0
for j in lojs:
    a1average += j["grp"]["a1"] # getting "a1" inside of "grp"
a1average = a1average/len(lojs)

如果你想要得到a1,你可以用a2代替a2

扩展,用于可能具有不同数量的“a”:的文档

代码语言:javascript
复制
doc1 = {"count": 157065, "grp": {"a1": 12, "a2": 32}}
doc2 = {"count": 2342, "grp": {"a1": 4, "a2": 34}}
doc3 = {"count": 543, "grp": {"a1": 1, "a2": 11, "a3": 46, "a4": 23}}
doc4 = {"count": 156, "grp": {"a1": 56, "a2": 75, "a3": 23}}
lojs = [doc1, doc2, doc3, doc4]

grps = [] # defining a list that will contain all of the "a"s
for doc in lojs: # getting each document in the list of documents
    for a in doc["grp"].keys(): # getting all the keys in the grp of that document
        if a not in grps: # checking whether the "a" already exists in the list of "a"s
            grps.append(a) # adding the new "a" to the list

averages = {} # using a dict instead of a list because it will be containing multiple values
for grp in grps: # getting each "a"
    averages[grp] = [0, 0] # setting the value of that "a" to zero

for grp in grps: # getting each "a"
    for doc in lojs: # getting each document
        if grp in doc["grp"].keys(): # getting every "a" in the grp of the document
            averages[grp][0] += doc["grp"][grp] # adding the value of that a to the corresponding value/key (idk dude) in the dictionary
            averages[grp][1] += 1 # increasing the amount the "a" has been mentioned by 1

for el in averages: # getting each average
    averages[el][0] = averages[el][0]/averages[el][1] # dividing b

并且您可以获得每个平均值的值

averages["a3"][0]

当然,您可以将"a3“更改为您想要的"a”。顺便说一句,如果不清楚,您将得到第一个元素,因为该键的值是一个列表,其中包含平均(idk,如果是一个单词)值和文档中发生"a“的次数。

这可能不是最有效的方法,但我的意思是,它是有效的!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70915456

复制
相关文章

相似问题

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