计算某年之前的平均值

时间:2019-07-08 11:36:32

标签: python

我想计算某个年份(例如2018年)之前列表的平均值。

import json
import numpy as np
from datetime import datetime
from datetime import date

with open('./output/del_gm_demo_full.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())

for b in data:
    results = b['results']
    b["avg_2018"] = []
    for a in results:
        date_pyt = a["auction_datetime"]
        date_datetime = datetime.strptime(
            date_pyt, "%Y-%m-%d").date()

        if not results == []:
            prices2018 = [a["price_int_eu"] for a in results]
            prices2018 = [a for a in prices2018 if a is not None]
            if not prices2018 == []:
                b["avg_2018"] = np.mean(prices2018)
                b["avg_2018"] = int(b["avg_2018"])
        else:
            b["avg_actual_2018"] = None

此代码适用于所有结果。我必须使用.MAXYEAR吗?我该怎么办?

我的示例的输出:

[{  "objectID": 1,
    "results": [
                {
                    "auction_date": "8 février 2019",
                    "auction_datetime": "2019-02-08",
                    "price_int_eu": 133504
                },
                {
                    "auction_date": "24 juin 2018",
                    "auction_datetime": "2018-06-24",
                    "price_int_eu": 86000
                },
                {
                    "auction_date": "11 mai 2018",
                    "auction_datetime": "2018-05-11",
                    "price_int_eu": null
                },
                {
                    "auction_date": "9-10 février 2018",
                    "auction_datetime": "2018-02-10",
                    "price_int_eu": 113240
                }
    "avg_2018": 110915
},
{  "objectID": 2,
    "results": [
                {
                    "auction_date": "5 juillet 2019",
                    "auction_datetime": "2019-07-05",
                    "price_int_eu": 30000
                },
                {
                    "auction_date": "23 juin 2018",
                    "auction_datetime": "2018-06-23",
                    "price_int_eu": 40000
                },
                {
                    "auction_date": "11 mai 2018",
                    "auction_datetime": "2018-05-11",
                    "price_int_eu": null
                },
                {
                    "auction_date": "9-10 février 2018",
                    "auction_datetime": "2018-02-10",
                    "price_int_eu": 35000
                }
    "avg_2018": 35000
}]

编辑:除了avg_2018键以外,我的输入是相同的JSON文件。此外,我得到了很多结果,这就是为什么我的第一行是通过results的循环(我简化了问题代码)

0 个答案:

没有答案