Home > Software design >  Pandas filter with month
Pandas filter with month

Time:10-06

I have a dataframe that looks like below

Date                          3tier1      2tier1      2tier2      2tier3
2001-12-01 08:00:00 08:00   64.2946632  47.25716228 17.04223722 64.2946632
2001-12-02 08:00:00 08:00   63.87557277 47.28039931 16.59856272 63.87557277
2001-12-03 08:00:00 08:00   63.43537544 47.29749771 16.14010007 63.43537544
2001-12-04 08:00:00 08:00   62.97632974 47.30827399 15.66932265 62.97632974
2001-12-05 08:00:00 08:00   62.50030363 47.31253544 15.18832366 62.50030363
2001-12-06 08:00:00 08:00   62.0088254  47.31007936 14.6988683  62.0088254
2001-12-07 08:00:00 08:00   61.50312807 47.30069244 14.20243945 61.50312807
2001-12-08 08:00:00 08:00   60.98418775 47.28415013 13.70027744 60.98418775
2001-12-09 08:00:00 08:00   60.45275675 47.26021591 13.19341477 60.45275675
2002-05-12 08:00:00 08:00   60.06028069 45.43305289 14.67607992 60.06028069
2002-05-13 08:00:00 08:00   60.89978183 45.34347401 15.60584812 60.89978183
2002-05-14 08:00:00 08:00   61.68801293 45.24973017 16.48786436 61.68801293
2002-05-15 08:00:00 08:00   62.42284657 45.15191829 17.31994301 62.42284657
2002-05-16 08:00:00 08:00   63.10251078 45.05013361 18.10025949 63.10251078
2002-05-17 08:00:00 08:00   63.72553989 44.94447026 18.82729998 63.72553989
2002-05-18 08:00:00 08:00   64.29072138 44.83502163 19.49980764 64.29072138
2002-05-19 08:00:00 08:00   64.79704208 44.7218804  20.11672858 64.79704208
2002-06-03 08:00:00 08:00   64.79007123 42.64210728 22.14835854 64.79007123
2002-06-04 08:00:00 08:00   64.24049486 42.48172309 21.76076277 64.24049486
2002-06-05 08:00:00 08:00   63.61590394 42.31904254 21.30190026 63.61590394

The above data is actually filtered from another dataframe using the below code

df = df[df['3tier1'].between(60, 65)]

I wanted to filter values between 60 and 65 using column 3tier1 and used the above code. Now, i want a filter to pick up data only for the months May and June i.e month 5 and 6 from the date column from the above filtered data. Is there a pandas function that can do this easily. Date column contains time as well 08:00:00 08:00 , this is basically 8 am in timezone 8

CodePudding user response:

To filter on month, you can pass the column series to pd.to_datetime then access month by dt.month then use isin method and pass list of months you are interested in i.e. 5 and 6:

df[df['3tier1'].between(60, 65) & pd.to_datetime(df['Date']).dt.month.isin([5,6])]

                         Date     3tier1     2tier1     2tier2     2tier3
9   2002-05-12 08:00:00 08:00  60.060281  45.433053  14.676080  60.060281
10  2002-05-13 08:00:00 08:00  60.899782  45.343474  15.605848  60.899782
11  2002-05-14 08:00:00 08:00  61.688013  45.249730  16.487864  61.688013
12  2002-05-15 08:00:00 08:00  62.422847  45.151918  17.319943  62.422847
13  2002-05-16 08:00:00 08:00  63.102511  45.050134  18.100259  63.102511
14  2002-05-17 08:00:00 08:00  63.725540  44.944470  18.827300  63.725540
15  2002-05-18 08:00:00 08:00  64.290721  44.835022  19.499808  64.290721
16  2002-05-19 08:00:00 08:00  64.797042  44.721880  20.116729  64.797042
17  2002-06-03 08:00:00 08:00  64.790071  42.642107  22.148359  64.790071
18  2002-06-04 08:00:00 08:00  64.240495  42.481723  21.760763  64.240495
19  2002-06-05 08:00:00 08:00  63.615904  42.319043  21.301900  63.615904
  • Related