【问题标题】:Python SumIfs for list of list dates用于列表日期列表的 Python SumIfs
【发布时间】:2016-11-30 01:04:30
【问题描述】:

我有一个列表列表,该列表由 excel 浮点格式的日期(自 1996 年 7 月 5 日以来的每分钟)和一个与每个日期相关联的整数值组成,如下所示:[[datetime,integer]...]。我需要创建一个由所有日期(没有小时或分钟)和该日期内所有日期时间的值总和组成的新列表。换句话说,当listolists[x][0] >= math.floor(listolists[x][0])listolists[x][0] < math.floor(listolists[x][0]) 时,每个日期的值的总和是多少。谢谢

【问题讨论】:

  • “该日期内的日期时间”是什么意思?
  • 我认为她想对给定日期的所有按分钟值求和。

标签: python excel list dictionary python-datetime


【解决方案1】:

由于您没有提供任何实际数据(只是您使用的数据结构、嵌套列表),我在下面创建了一些虚拟数据来演示您如何在 Python 中解决SUMIFS 类型的问题。

from datetime import datetime
import numpy as np
import pandas as pd

dates_list = []

# just take one month as an example of how to group by day
year = 2015
month = 12

# generate similar data to what you might have
for day in range(1, 32):
    for hour in range(1, 24):
        for minute in range(1, 60):
            dates_list.append([datetime(year, month, day, hour, minute), np.random.randint(20)])

# unpack these nested list pairs so we have all of the dates in 
# one list, and all of the values in the other
# this makes it easier for pandas later
dates, values = zip(*dates_list)

# to eventually group by day, we need to forget about all intra-day data, e.g.
# different hours and minutes. we only care about the data for a given day, 
# not the by-minute observations. So, let's set all of the intra-day values to
# some constant for easier rolling-up of these dates.
new_dates = []

for d in dates:
    new_d = d.replace(hour = 0, minute = 0)
    new_dates.append(new_d)

# throw the new dates and values into a pandas.DataFrame object
df = pd.DataFrame({'new_dates': new_dates, 'values': values})

# here's the SUMIFS function you're looking for
grouped = df.groupby('new_dates')['values'].sum()

让我们看看结果:

>>> print(grouped.head())
new_dates
2015-12-01    12762
2015-12-02    13292
2015-12-03    12857
2015-12-04    12762
2015-12-05    12561
Name: values, dtype: int64

编辑:如果您希望这些新的分组数据返回嵌套列表格式,只需执行以下操作:

new_list = [[date, value] for date, value in zip(grouped.index, grouped)]

【讨论】:

    【解决方案2】:

    谢谢大家。这是我能想到的不需要熊猫的最简单的代码:

    for row in listolist:
         for k in (0, 1):
             row[k] = math.floor(float(row[k]))
    date = {}
    for d,v in listolist:
        if d in date:
            date[math.floor(d)].append(v)
        else:
            date[math.floor(d)] = [v]
    result = [(d,sum(v)) for d,v in date.items()] 
    

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 1970-01-01
      • 2017-09-10
      • 2021-06-04
      • 2021-09-29
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多