2016-11-30 57 views
0

我有一個由excel浮動格式日期(1996年7月5日以來的每分鐘)組成的列表列表以及與每個日期相關的整數值,如下所示:[[datetime,integer]...]。我需要創建一個由所有日期(無小時或分鐘)組成的新列表以及該日期內所有日期時間值的總和。換句話說,當listolists[x][0] >= math.floor(listolists[x][0])listolists[x][0] < math.floor(listolists[x][0])時,每個日期的值的總和是多少。謝謝列表日期列表的Python SumIfs

+2

你所說的「該日期內的日期時間」是什麼意思? – rassar

+0

我認爲她想總結某一天的所有分鐘值。 – blacksite

回答

0

由於您沒有提供任何實際數據(只是您使用的數據結構,嵌套列表),我在下面創建了一些虛擬數據來演示如何在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)] 
0

謝謝大家。這是我能想出不需要熊貓最簡單的代碼:

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()]