apply
這裏是你的朋友
def sum_from_months_prior(row, df):
'''returns sum of values in row month,
from all dates in df prior to row date'''
month = pd.to_datetime(row).month
all_dates_prior = df[df.index <= row]
same_month = all_dates_prior[all_dates_prior.index.month == month]
return same_month["values"].sum()
data = {'dates': ['2010-01-29', '2011-06-14', '2012-01-18'], 'values': [4, 3, 8]}
df = pd.DataFrame(data)
df.set_index('dates', inplace = True)
df.index = pd.to_datetime(df.index)
df["dates"] = df.index
df.sort_index(inplace = True)
df["Month"] = df["dates"].apply(lambda row: sum_from_months_prior (row, df))
df.drop("dates", axis = 1, inplace = True)
所需DF:
values Month
dates
2010-01-29 4 4
2011-06-14 3 3
2012-01-18 8 12
這些都是對應其d值ay,4代表'2010-01-29',8代表'2012-01-18' – user6162407