Using Pandas to filter 2 specific day of year

Viewed 48

I have a big CSV dataset and i wish to filter my dataset with use of Pandas and save it into new CSV File

The aim is to find all the records for 1 and 15 days of the year

when i used following code it is work

print (df[(df['data___date_time'].dt.day == 1)]) 

and result appear as follow:

         data___date_time       NO2       SO2   PM10
26    2020-07-01 00:00:00  1.591616  0.287604    NaN
27    2020-07-01 01:00:00  1.486401       NaN    NaN
28    2020-07-01 02:00:00  1.362056       NaN    NaN
29    2020-07-01 03:00:00  1.295101  0.194399    NaN
30    2020-07-01 04:00:00  1.260667  0.362168    NaN
                  ...       ...       ...    ...
17054 2022-07-01 19:00:00  2.894369  2.077140  19.34
17055 2022-07-01 20:00:00  3.644265  1.656386  23.09
17056 2022-07-01 21:00:00  2.907760  1.291555  23.67
17057 2022-07-01 22:00:00  2.974715  1.318185  27.68
17058 2022-07-01 23:00:00  2.858022  1.169057  25.18

However when i used following code nothing comes out

print (df[(df['data___date_time'].dt.day == 1) & (df['data___date_time'].dt.day == 15)])     

this just gave me:

Empty DataFrame
Columns: [data___date_time, NO2, SO2, PM10]
Index: []

Is there any idea what could be the problem

1 Answers

There is logical problem, is not possible same row 1 and 15, need | for bitwise OR. If need test multiple values simplier is use Series.isin:

df = pd.DataFrame({'data___date_time': pd.date_range('2000-01-01', periods=20)})

print (df[df['data___date_time'].dt.day.isin([1,15])]) 
   data___date_time
0        2000-01-01
14       2000-01-15
Related