gpt4 book ai didi

python - Pandas Dataframe 根据日期过滤行

转载 作者:行者123 更新时间:2023-12-01 01:18:54 25 4
gpt4 key购买 nike

我正在尝试根据 pandas 数据框的日期列过滤行。我已将去年和上个月存储在变量中,现在想要根据该年份和月份过滤行。

这是数据框。 enter image description here

我存储在以下变量中的年份和月份。

now = datetime.datetime.now()
last_month = now.month - 1 if now.month > 1 else 12
last_year = now.year - 1

如何过滤包含last_year 和last_month 的行?

最佳答案

我认为你需要boolean indexingdt.yeardt.month :

df[(df['Date'].dt.year == last_year) & (df['Date'].dt.month == last_month)]

关于python - Pandas Dataframe 根据日期过滤行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54034503/

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