简体   繁体   中英

Loop for starting month and starting year to end month and year python

Database df:

month    year   data
Jan      2017    ggg
Feb      2015    jhjj
Jan      2018    hjhj
Mar      2018    hjhj

and so on

Code:

def data_from_start_month_to_end_month:
    for y in range(start_year,end_year):
        do something 
        for m in range(start_month,13):
            df = df[(df['month'] == m)&(df['year']== y)]
    return df        

This will start the code from the starting month and year but what if end month is not December, then it wont work.

Output I want:

start_month = Sep 
start_year = 2000
end_month = Feb
end_year = 2019 say

So loop should work from Sep 2000 to Feb 2019 and extract the data only for those rows. (but I need the function to be generic and not hard coded

Can anyone help

You can use the below function which uses series.between after converting the inputs to datetime:

def myf(df,start_month,start_year,end_month,end_year):
    s= pd.to_datetime(df['month']+df['year'].astype(str),format='%b%Y')
    start = pd.to_datetime(start_month+str(start_year),format='%b%Y')
    end = pd.to_datetime(end_month+str(end_year),format='%b%Y')
    return df[s.between(start,end)]

myf(df,'Sep',2000,'Feb',2017)

  month  year  data
0   Jan  2017   ggg
1   Feb  2015  jhjj

If month is number , use format='%m%Y' instead of format='%b%Y' :

def myf1(df,start_month,start_year,end_month,end_year):
    s= pd.to_datetime(df['month'].astype(str)+df['year'].astype(str),format='%m%Y')
    start = pd.to_datetime(start_month+str(start_year),format='%b%Y')
    end = pd.to_datetime(end_month+str(end_year),format='%b%Y')
    return df[s.between(start,end)]

Example df:

   month  year  data
0      1  2017   ggg
1      2  2015  jhjj
2      1  2018  hjhj
3      3  2018  hjhj

myf1(df,'Sep',2000,'Feb',2017)

   month  year  data
0      1  2017   ggg
1      2  2015  jhjj

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