给定一个小数据集如下:
id date
0 1 2020-01-01
1 2 2020-12-02
2 3 2020-09-26
3 4 2020-05-04
4 5 2020-01-05
我想检查date
从现在起是否在3个月的范围内(从今天起,如果是2020-12-25
,则范围将为[2020-09-25, 2020-12-25]
(,如果不是,则使用N
返回新列check
。
预期结果会像:
id date check
0 1 2020-01-01 N
1 2 2020-12-02 NaN
2 3 2020-09-26 NaN
3 4 2020-05-04 N
4 5 2020-01-05 N
我怎么能在Python中做到这一点?谢谢
以下解决方案有效:
import datetime
import dateutil.relativedelta
start_date = (datetime.datetime.now() + dateutil.relativedelta.relativedelta(months=-3)).strftime('%Y-%m-%d')
end_date = datetime.datetime.now().strftime('%Y-%m-%d')
mask = df['date'].between(start_date, end_date, inclusive=True)
df.loc[~mask, 'check'] = 'N'
输出:
id date check
0 1 2020-01-01 N
1 2 2020-12-02 NaN
2 3 2020-09-26 NaN
3 4 2020-05-04 N
4 5 2020-01-05 N