提取 Pandas DF 中特定年份的行

Extract Rows with Year(s) Specific in Pandas DF

我有一个 df“cdata”,其形状为 (4743816,7),如下所示:

    plant_name business_name maint_region_name wind_speed_ms  \
0  RIO DO FOGO        BRAZIL            BRAZIL          8.72   
1  RIO DO FOGO        BRAZIL            BRAZIL          8.66   
2  RIO DO FOGO        BRAZIL            BRAZIL          8.68   
3  RIO DO FOGO        BRAZIL            BRAZIL          8.72   
4  RIO DO FOGO        BRAZIL            BRAZIL          8.65   

             mos_time power_kwh dataset  
0 2021-10-31 23:00:00   21250.9    ERA5  
1 2021-10-31 22:00:00   21378.1    ERA5  
2 2021-10-31 21:00:00   22633.7    ERA5  
3 2021-10-31 20:00:00   22735.9    ERA5  
4 2021-10-31 19:00:00   23301.6    ERA5

mos_time 年是从 1991-01-01 00:00:00 到 2021-10-31 23:00:00。我需要创建新的 pandas df's with just years == 2021 and a second df with years not equal to the current year (2021) or 1991-2020.

我试过了,但它创建了一个空数据框:

import datetime as dt
years = [ '1991','1992','1993','1994','1995','1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006','2007',
         '2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018', '2019', '2020','2021']
yearsc = years[-1:] #need current year
df1 = cdata[cdata['mos_time'].dt.year.isin(yearsc)]

yearslt = years
del yearslt[-1]
df2 = cdata[cdata['mos_time'].dt.year.isin(yearslt)] 

使用上面的代码,我的 dfs (df1, df2) 为空,但不确定为什么。谢谢,

你可以这样做:

import datetime

curr_year = datetime.datetime.now().year
df1 = cdata[cdata['mos_time'].dt.year.eq(curr_year)]
df2 = cdata[cdata['mos_time'].dt.year.ne(curr_year)]