gpt4 book ai didi

python - Pandas 数据框按日期范围过滤 : today - today+ 1 year

转载 作者:行者123 更新时间:2023-11-28 20:16:21 35 4
gpt4 key购买 nike

我可以使用日期范围过滤数据框:

df[(df['Due Date'] >= '2017-01-01') & (df['Due Date'] <= '2017-02-01')]

但我希望能够过滤一年

最佳答案

IIUC 你可以这样做:

In [99]: from dateutil.relativedelta import relativedelta

In [100]: today = pd.datetime.today()

In [101]: today_next_year = today + relativedelta(years=1)

In [102]: df.loc[df['Due Date'].between(today, today_next_year)]
Out[102]:
Due Date OtherColumn
9 2017-06-30 9
10 2017-09-30 10
11 2017-12-31 11
12 2018-03-31 12

关于python - Pandas 数据框按日期范围过滤 : today - today+ 1 year,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43416060/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com