简体   繁体   中英

select pandas dataframe between two given dates where values from two columns are equal

I'm able to select pandas dataframe between two dates by first setting a datetime created column as index and slicing the dataframe. But now I want to do a new query involving an additional datetime column 'modifieddate' ie:

df = df.set_index(['created'])
print (df)
                        name      modifieddate
created                                                        
2014-01-01 16:07:07     john      2014-01-01 16:07:07
2014-01-04 16:07:07     harold    2014-01-04 16:07:07
2014-01-04 16:07:07     clara     2014-01-04 18:07:07
2014-01-05 16:07:07     emily     2014-01-06 16:07:07
2014-01-08 16:07:07     smiths    2014-01-08 16:07:07
2014-01-09 20:07:07     clara     2014-01-09 20:07:07
2014-01-10 18:07:07     clara     2014-01-10 18:07:07
2014-01-10 16:07:07     john      2014-01-11 16:07:07

select rows where created and modifieddate are equal and falls between given datetimes 2014-01-04 16:07:07 and 2014-01-10 16:07:07 :

                        name      modifieddate
created                                                        
2014-01-04 16:07:07     harold    2014-01-04 16:07:07 
2014-01-08 16:07:07     smiths    2014-01-08 16:07:07
2014-01-09 20:07:07     clara     2014-01-09 20:07:07

You can use between with boolean indexing :

s = '2014-01-04 16:07:07'
e = '2014-01-10 16:07:07'
df = df[(df.index.to_series().between(s,e)) & 
        (df.modifieddate.between(s,e)) & 
        (df.index == df.modifieddate)]
print (df)
                       name        modifieddate
created                                        
2014-01-04 16:07:07  harold 2014-01-04 16:07:07
2014-01-08 16:07:07  smiths 2014-01-08 16:07:07
2014-01-09 20:07:07   clara 2014-01-09 20:07:07

Assuming your column 'created' is not the index.

 df2=     df.ix[(df.created==df.modifieddate)&(df.created>=datetime.datetime(2014,1,4,
 16,7,7))&(df.created <=datetime.datetime(2014,1,10, 16,7,7)]

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