繁体   English   中英

基于日期的熊猫数据框过滤器行

[英]Pandas Data frame Filter Rows of Based on date

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

这是数据帧。 在此处输入图片说明

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

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

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

我认为您需要使用dt.yeardt.month boolean indexing

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM