简体   繁体   中英

Pandas Data frame Filter Rows of Based on date

I'm trying to filter rows based on date column of the pandas' data frame. I've stored last year and last month in variable now wanted to filter rows based on that year and month.

Here is the data frame. 在此处输入图片说明

The Year and month I've stored in below variables.

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

How can I filter rows which contain last_year and last_month?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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