0

我有一个形状为(4743816,7)的df“cdata”,看起来像这样:

    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,只有年份 == 2021 和第二个 df,其年份不等于当前年份(2021 年)或 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) 并且不知道为什么。谢谢你,

4

1 回答 1

1

你可以这样做:

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)]
于 2021-11-16T18:34:50.280 回答