我有如下数据集
Transaction Date Amount
0 2014-05-19 85.54
1 2014-01-14 850.00
2 2014-03-31 25.00
3 2013-09-27 82.00
4 2013-10-17 16.44
... ... ...
358481 2014-06-11 3573.87
358482 2013-08-14 62.01
358483 2013-07-16 181.80
358484 2013-12-12 98.85
358485 2013-11-05 49.95
[358486 rows x 2 columns]
我知道如何获取当前时间前 N
天的值
df.loc[df["Transaction Date"] > datetime.datetime.now() - datetime.timedelta(30), ["Amount"]]
但我想要做的是在每个特定行的 Amount
之前获得过去 N
天的平均 Transaction Date
。
我该怎么做?
**
注意
df.dtypes
的结果
Amount float64
Transaction Date datetime64[ns]
dtype: object
预期输出
average amount
0 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
1 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
2 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
3 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
4 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
... ...
358481 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
358482 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
358483 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
358484 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
358485 AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
[358486 rows x 1 columns]
AVARAGE_AMOUNT_LAST_N_DAYS_BEFORE_THIS_ROW_AS_FLOAT
如果在 0
之前 30 天没有交易,则此值应为 Transaction Date
。
df.dtypes
average amount float64
dtype: object
不一定是DataFrame,也可以是list。