Python中使用日期时间的每月总金额

时间:2018-08-15 16:57:25

标签: python list datetime sum

现在我有了日期和值的列表,但是我不知道如何使用日期结构进行计算。

看起来

[[datetime.date(2018, 8, 10) 1076.2392505636847]
 [datetime.date(2018, 8, 11) 3537.9781979862732]
 [datetime.date(2018, 8, 12) 8637.536518161462]
 [datetime.date(2018, 8, 13) 15660.768121458246]
 [datetime.date(2018, 8, 14) 21087.477911830327]
 [datetime.date(2018, 8, 15) 21087.477911830327]
 [datetime.date(2018, 8, 16) 15660.768121458246]
 [datetime.date(2018, 8, 17) 8637.536518161465]
 [datetime.date(2018, 8, 18) 3537.9781979862732]
 [datetime.date(2018, 8, 19) 1076.2392505636856]]

另外,我知道

startdate = datetime.date(2018, 8, 10)
enddate = datetime.date(2018,8, 19)

我想创建另一个列表,该列表由['Year-Month'数据,月份的总和]组成。在这种情况下,将仅为['2018-8'总金额]。如果结束日期为2020,8,19,则长度为25(两年零一个月)。

您能分享一些我可能会使用的有用功能/方法吗?

4 个答案:

答案 0 :(得分:4)

collections.defaultdict

您可以将collections.defaultdict用于不需要排序的O(n)解决方案。

import datetime

L = [[datetime.date(2018, 8, 10), 1076.23], [datetime.date(2018, 8, 11), 3537.97],
     [datetime.date(2018, 8, 19), 1076.23], [datetime.date(2018, 9, 10), 5.23],
     [datetime.date(2018, 9, 11), 10.97], [datetime.date(2018, 10, 19), 15.23]]

from collections import defaultdict

d = defaultdict(int)

for date, val in L:
    d[date.strftime('%Y-%m')] += val

# defaultdict(int,
#             {'2018-08': 5690.43,
#              '2018-09': 16.20,
#              '2018-10': 15.23})

res = list(map(list, d.items()))

print(res)

[['2018-08', 5690.43],
 ['2018-09', 16.20],
 ['2018-10', 15.23]]

熊猫

如果您愿意使用第三方库,则可以使用熊猫:

# construct dataframe from list of lists
df = pd.DataFrame(L, columns=['date', 'val'])

# convert to datetime
df['date'] = pd.to_datetime(df['date'])

# perform GroupBy operation over monthly frequency
res = df.set_index('date').groupby(pd.Grouper(freq='M'))['val'].sum().reset_index()

print(res)

        date      val
0 2018-08-31 5690.430
1 2018-09-30   16.200
2 2018-10-31   15.230

答案 1 :(得分:1)

您可以使用minmax查找开始时间和结束时间。然后使用itertools.groupby对每个月的条目进行分组,并找到每个组的总和

lst = [[datetime.date(2018, 8, 10), 1076.2392505636847],
 [datetime.date(2018, 8, 11), 3537.9781979862732],
 [datetime.date(2018, 8, 12), 8637.536518161462],
 [datetime.date(2018, 8, 13), 15660.768121458246],
 [datetime.date(2018, 8, 14), 21087.477911830327],
 [datetime.date(2018, 8, 15), 21087.477911830327],
 [datetime.date(2018, 8, 16), 15660.768121458246],
 [datetime.date(2018, 8, 17), 8637.536518161465],
 [datetime.date(2018, 8, 18), 3537.9781979862732],
 [datetime.date(2018, 8, 19), 1076.2392505636856]]

starttime = min(lst)
endtime   = max(lst)

from itertools import groupby
from operator import itemgetter
res = [[k.strftime('%Y-%m'), sum(map(itemgetter(1), group))] for k,group in groupby(lst, lambda sl: sl[0].replace(day=1))]
print (starttime, endtime)
print (res)

输出

[datetime.date(2018, 8, 10), 1076.2392505636847] [datetime.date(2018, 8, 19), 1076.2392505636856]
[['2018-08', 99999.99999999999]]

答案 2 :(得分:0)

如上所述,您应该尝试熊猫 您可以查看here的简介或 here进行类似的讨论

答案 3 :(得分:0)

有了熊猫,它将更加直观和易于理解

将数据加载到数据框中

df=pd.DataFrame([[datetime.date(2018, 8, 10), 1076.2392505636847],
                 [datetime.date(2018, 8, 11), 3537.9781979862732],
                 [datetime.date(2018, 8, 12), 8637.536518161462],
                 [datetime.date(2018, 8, 13), 15660.768121458246],
                 [datetime.date(2018, 8, 14), 21087.477911830327],
                 [datetime.date(2018, 8, 15), 21087.477911830327],
                 [datetime.date(2018, 8, 16), 15660.768121458246],
                 [datetime.date(2018, 8, 17), 8637.536518161465],
                 [datetime.date(2018, 8, 18), 3537.9781979862732],
                 [datetime.date(2019, 8, 19), 1076.2392505636856]], 
                 columns=["Date",'amount'])

将日期列转换为日期时间

df.Date=pd.to_datetime(df.Date)

按年和月创建索引

df.index=[df.Date.dt.year, df.Date.dt.month]

按年和月汇总

df.groupby(['year','month']).sum()