royalewithcheese
royalewithcheese

Reputation: 502

How to filter columns on the basis of a condition using pandas/python?

Supposing I have a dataframe (df) consisting columns of months-years, for instance,

column1| column2 | column3 | Jan-20 | Feb-20 | Mar-20 | ..... | Nov-21| Dec-21 

I want to filter the columns on the basis of following conditions,

  1. If the current month-year, datetime.now().strftime('%b-%y') is any one of the following,

example1

Then filter the following month-year columns from the dataframe (df) to,

solution1

For example, if we take datetime.now().strftime('%b-%y') as Mar-21, then according to the above condition the final columns in df would be,

column1 | column2 | column3 | Mar-21 | Apr-21 | May-21 | .....| Aug-21 | Sep-21
  1. If the current month-year, datetime.now().strftime('%b-%y') is any one of the following,

example2

Then filter the following month-year columns from the dataframe (df) to,

solution2

For example, if we take datetime.now().strftime('%b-%y') as Sep-21, then according to the above condition the final columns in df would be,

column1 | column2 | column3 | Oct-21 | Nov-21 | Dec-21 | Jan-22 | Feb-22

Another example for better understanding of the logic,

For condition 1)

If datetime.now().strftime('%b-%y') lies in any one of the following current month-year, the filtered columns would be the following,

example3

For condition 2)

If datetime.now().strftime('%b-%y') lies in any one of the following current month-year, the filtered columns would be the following,

example3

and so on..

Is there a way to achieve this filtering on the basis of above condition using pandas/python?

Upvotes: 1

Views: 182

Answers (1)

Paras Gupta
Paras Gupta

Reputation: 174

I tried offsetting current date by 2 months to extract current year.

You can try using below function to generate filter dates.

from dateutil.relativedelta import relativedelta
import datetime

def generate_dates():
    offset = datetime.datetime.now() + relativedelta(months=2)
    count, st_month = (7,3) if offset.month in range(1,8) else (5, 10)

    st_date = datetime.datetime(offset.year, st_month, 1)
    res = []
    while len(res) < count:
        res.append(st_date.strftime("%b-%y"))
        st_date = st_date + relativedelta(months=1)
    return res

>>> generate_dates()
['Mar-21', 'Apr-21', 'May-21', 'Jun-21', 'Jul-21', 'Aug-21', 'Sep-21']

Below are outputs for different current dates:

Nov-21 : ['Mar-22', 'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22', 'Sep-22']
Dec-21 : ['Mar-22', 'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22', 'Sep-22']
Jan-22 : ['Mar-22', 'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22', 'Sep-22']
Feb-22 : ['Mar-22', 'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22', 'Sep-22']
Mar-22 : ['Mar-22', 'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22', 'Sep-22']
Apr-22 : ['Mar-22', 'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22', 'Sep-22']
May-22 : ['Mar-22', 'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22', 'Sep-22']
Jun-22 : ['Oct-22', 'Nov-22', 'Dec-22', 'Jan-23', 'Feb-23']
Jul-22 : ['Oct-22', 'Nov-22', 'Dec-22', 'Jan-23', 'Feb-23']
Aug-22 : ['Oct-22', 'Nov-22', 'Dec-22', 'Jan-23', 'Feb-23']
Sep-22 : ['Oct-22', 'Nov-22', 'Dec-22', 'Jan-23', 'Feb-23']
Oct-22 : ['Oct-22', 'Nov-22', 'Dec-22', 'Jan-23', 'Feb-23']

Upvotes: 1

Related Questions