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.
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.year
和dt.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.