Home > Back-end >  Extract Rows with Year(s) Specific in Pandas DF
Extract Rows with Year(s) Specific in Pandas DF

Time:11-17

I have a df "cdata" that is (4743816,7) in shape and looks like this:

    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

The mos_time years are from 1991-01-01 00:00:00 to 2021-10-31 23:00:00. I need to create new pandas df's with just years == 2021 and a second df with years not equal to the current year (2021) or 1991-2020.

I've tried this but it creates an empty dataframe:

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)] 

With the code above, I have empty dfs (df1, df2) and not sure why. thank you,

CodePudding user response:

You can do this:

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)]
  • Related