【问题标题】:Aggregating JSON values in Python在 Python 中聚合 JSON 值
【发布时间】:2018-02-01 18:31:12
【问题描述】:

我想知道是否可以在 python 中将 JSON 数据聚合成新值。

例如,单个 JSON 值如下所示:

{"time": {"Friday": {"20:00": 2, "19:00": 1, "22:00": 10, "21:00": 5, 
          "23:00": 14, "0:00": 2, "18:00": 2}, "Thursday": {"23:00": 1, 
          "0:00": 1, "19:00": 1, "18:00": 1, "16:00": 2, "22:00": 2},
          "Wednesday": {"17:00": 2, "23:00": 3, "16:00": 1, "22:00": 1, 
          "19:00": 1, "21:00": 1}, "Sunday": {"16:00": 2, "17:00": 2, "19:00": 1, 
          "22:00": 4, "21:00": 4, "0:00": 3, "1:00": 2}, "Saturday": 
          {"21:00": 4, "20:00": 3, "23:00": 10, "22:00": 7, "18:00": 
          1, "15:00": 2, "16:00": 1, "17:00": 1, "0:00": 8, "1:00": 
          1}, "Tuesday": {"19:00": 1, "17:00": 1, "1:00": 2, "21:00": 
          1, "23:00": 3}, "Monday": {"18:00": 2, "23:00": 1, "22:00": 2}}

我想根据打开时间将其汇总为四个类别。

四个类别是:

早上 6 点 - 中午 12 点:早上

中午 12 点 - 下午 5 点:下午

下午 5 点 - 晚上 11 点:晚上

晚上 11 点 - 早上 6 点:晚上

例如:

如果这是当前值:

“Friday”:{“20:00”: 5,“21:00”: 10}

那么输出应该是:

"Friday": {"morning": 0, "afternoon": 0, "evening": 15, "night": 0}

因此输出应该是格式

"Day": {"morning": count, "afternoon": count, "evening": count, "night":count}

对于所有数百个 JSON 值。

我的想法是我可以创建代表每个时区的 4 个 bin。然后我会使用两个 for 循环来遍历每一天的值。如果该值在桶的范围内,我会将其添加到计数中。然后我会将这一天存储在字典中,其中的值也是字典。内部字典由四个时区组成,计数为值。然后我会在当天返回这个并重新开始每一天。

到此为止,还需要实现聚合函数。

import json
from datetime import datetime

def cleanStr4SQL(s):
    return s.replace("'","`").replace("\n"," ")

def parseCheckinData():
    #write code to parse yelp_checkin.JSON
    with open('yelp_checkin.JSON') as f:
        outfile = open('checkin.txt', 'w')
        line = f.readline()
        count_line = 0
        while line:
            data = json.loads(line)
            outfile.write(cleanStr4SQL(str(data['business_id'])) + '\t')
            outfile.write(aggregate(cleanStr4SQL(str(data['time']))))

            line = f.readline()
            count_line+=1
    print(count_line)
    outfile.close()
    f.close()

def aggregate(line):
    morning = []
    afternoon = []
    evening = []
    night = []
    for l in line:
        print(l)

我想知道在 python 中解决这个问题的最佳方法是什么。

感谢任何建议。我知道没有代码,但如果有人能指出我的方向,那就太好了。

感谢您的阅读

【问题讨论】:

  • 您正在处理时间序列类型的数据。尝试寻找一个特定的包来处理它。例如,mongodb 具有处理数据聚合的本地方法。
  • 有趣,我会研究一下。也只是想知道,但你知道迭代这个 JSON 值的最佳方法是什么吗?谢谢!
  • 乍一看,我会每天迭代并创建新的类别(早上、晚上等)。

标签: python sql json database file


【解决方案1】:

这是处理它的一种可能方法。我只用一个 json 字符串进行了尝试,因此您可能必须对其进行扩展以处理多次出现。

import json
import pandas as pd

jsontxt = '{"time": {"Friday": {"20:00": 2, "19:00": 1, "22:00": 10, "21:00": 5, "23:00": 14, "0:00": 2, "18:00": 2}, "Thursday": {"23:00": 1, "0:00": 1, "19:00": 1, "18:00": 1, "16:00": 2, "22:00": 2}, "Wednesday": {"17:00": 2, "23:00": 3, "16:00": 1, "22:00": 1, "19:00": 1, "21:00": 1}, "Sunday": {"16:00": 2, "17:00": 2, "19:00": 1, "22:00": 4, "21:00": 4, "0:00": 3, "1:00": 2}, "Saturday": {"21:00": 4, "20:00": 3, "23:00": 10, "22:00": 7, "18:00": 1, "15:00": 2, "16:00": 1, "17:00": 1, "0:00": 8, "1:00": 1}, "Tuesday": {"19:00": 1, "17:00": 1, "1:00": 2, "21:00": 1, "23:00": 3}, "Monday": {"18:00": 2, "23:00": 1, "22:00": 2}}}'

# Parse the json and convert to a dictionary object
jsondict = json.loads(jsontxt)

# Convert the "time" element in the dictionary to a pandas DataFrame
df = pd.DataFrame(jsondict['time'])

# Define a function to convert the time slots to the categories
def cat(time_slot):
    if '06:00' <= time_slot < '12:00':
        return 'Morning'
    elif '12:00' <= time_slot < '17:00':
        return 'Afternoon'
    elif '17:00' <= time_slot < '23:00':
        return 'Evening'
    else:
        return 'Night'

# Add a new column "Time" to the DataFrame and set the values after left padding the values in the index
df['Time'] = df.index.str.rjust(5,'0')

# Add a new column "Category" and the set the values based on the time slot
df['Category'] = df['Time'].apply(cat)

# Create a pivot table based on the "Category" column
pt = df.pivot_table(index='Category', aggfunc=sum, fill_value=0)

# Convert the pivot table to a dictionary to get the json output you want
jsonoutput = pt.to_dict()
print(jsonoutput)

希望有帮助

【讨论】:

  • 太棒了!我没有想到使用 pandas 库。感谢您的帮助!
  • 感谢您的赞赏。很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 2017-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多