python - 使用python按周汇总数据

标签 python list datetime ubuntu aggregation

我有这个数据,我不会在给定的两个日期期间按周汇总它: 我确实有这个元组列表,显示我的交易表中每天的销售额

Metadate = [(Day, 'totalSales By dollars')]

--

data=[('2013-06-21', 14),
 ('2013-06-20', 19),
 ('2013-06-23', 11),
 ('2013-06-22', 13),
 ('2013-06-25', 6),
 ('2013-06-24', 22),
 ('2013-06-27', 22),
 ('2013-06-26', 3),
 ('2013-06-29', 10),
 ('2013-06-28', 15),
 ('2013-04-05', 20),
 ('2013-04-04', 33),
 ('2013-04-07', 20),
 ('2013-04-06', 15),
 ('2013-04-01', 23),
 ('2013-04-03', 5),
 ('2013-04-02', 8),
 ('2013-04-09', 10),
 ('2013-04-08', 24),
 ('2013-05-08', 1),
 ('2013-05-09', 29),
 ('2013-05-02', 17),
 ('2013-05-03', 18),
 ('2013-05-01', 4),
 ('2013-05-06', 16),
 ('2013-05-07', 10),
 ('2013-05-04', 9),
 ('2013-05-05', 12),
 ('2013-05-19', 21),
 ('2013-05-18', 26),
 ('2013-05-11', 8),
 ('2013-05-10', 12),
 ('2013-05-13', 24),
 ('2013-05-12', 9),
 ('2013-05-15', 5),
 ('2013-05-14', 7),
 ('2013-05-17', 20),
 ('2013-05-16', 36),
 ('2013-05-20', 24),
 ('2013-05-21', 5),
 ('2013-05-22', 3),
 ('2013-05-23', 18),
 ('2013-05-24', 8),
 ('2013-05-25', 11),
 ('2013-05-26', 9),
 ('2013-05-27', 13),
 ('2013-05-28', 4),
 ('2013-05-29', 7),
 ('2013-06-18', 9),
 ('2013-06-19', 2),
 ('2013-06-10', 20),
 ('2013-06-11', 4),
 ('2013-06-12', 3),
 ('2013-06-13', 25),
 ('2013-06-14', 16),
 ('2013-06-15', 10),
 ('2013-06-16', 11),
 ('2013-06-17', 17),
 ('2013-04-30', 12),
 ('2013-05-31', 13),
 ('2013-05-30', 29),
 ('2013-06-09', 12),
 ('2013-06-08', 20),
 ('2013-06-07', 47),
 ('2013-06-06', 5),
 ('2013-06-05', 2),
 ('2013-06-04', 3),
 ('2013-06-03', 32),
 ('2013-06-02', 13),
 ('2013-06-01', 9),
 ('2013-04-23', 3),
 ('2013-04-22', 33),
 ('2013-04-21', 14),
 ('2013-04-20', 20),
 ('2013-04-27', 15),
 ('2013-04-26', 17),
 ('2013-04-25', 21),
 ('2013-04-24', 1),
 ('2013-04-29', 34),
 ('2013-04-28', 11),
 ('2013-06-30', 13),
 ('2013-04-16', 5),
 ('2013-04-17', 3),
 ('2013-04-14', 10),
 ('2013-04-15', 22),
 ('2013-04-12', 23),
 ('2013-04-13', 19),
 ('2013-04-10', 1),
 ('2013-04-11', 31),
 ('2013-04-18', 27),
 ('2013-04-19', 14)]

我想在给定开始和结束日期的两个日期按周汇总它:
输出示例:

[(2013-05-07, 900), [(2013-05-14, 1800),....., (2013-08-01, 1000)]

提前致谢!

最佳答案

试试这个:

from datetime import datetime
import itertools

sales = [
    ('2013-05-01', 100),
    ('2013-05-02', 200),
    ('2013-05-03', 150),
    ('2013-05-03', 120),
    ('2013-05-04', 200),
    ('2013-08-01', 250),
]

def toWeek(sale):
    '''(date,volume) -> date of the Sunday of that week'''
    sunday = datetime.strptime(sale[0], '%Y-%m-%d').strftime('%Y-%U-0')
    return datetime.strptime(sunday, '%Y-%U-%w').strftime('%Y-%m-%d')

grouped_sales = itertools.groupby(sales, toWeek)

aggregate_sales = (
    (week, sum(day_sales for date, day_sales in week_sales))
    for week, week_sales in grouped_sales)

print list(aggregate_sales)

关于python - 使用python按周汇总数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18797479/

相关文章:

python - django 1.4 如何从客户端自动获取用户的时区

python - 如何使用 Python 验证和访问特定 API

c# - NHibernate/Transaction 如何只提交一个对象而不提交整个 session

java - LocalDateTime Java - 获取下个月的同一天

python - 获得沿轴绝对值的最大值

python - 如何一次循环两个列表列表并将一个列表中的值替换为另一个列表中的值?

python - python中的list(set)操作是O(1)吗?

sql - 获取日期范围内的日期

c# - DocumentDB如何对_TS进行LINQ查询?

python - h5py 未针对 mpi4py 正确构建