Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Pandas DataFrame by comparing columns in a row

Each row in my DataFrame has two date columns. How can I filter out the rows in which 'Date A' is after 'Date B'?

Example:

symbol |    reports_at       |    as_of        |      signal   
...   
A     | 2012-02-15T21:00:00Z |  2012-02-01T12:00:00Z|   65.20464367   
...

This row should be deleted from the DataFrame because the date in the 'reports_at' column occurs after the date in the 'as_of' column

like image 298
Will Avatar asked Aug 09 '17 17:08

Will


1 Answers

You need boolean indexing or query:

1.

df1 = df[df['as_of'] > df['reports_at']]

2.

df1 = df.query('as_of > reports_at')

3.

df1 = df[df['reports_at'] <= df['as_of']]

4.

df1 = df.query('reports_at <= as_of')
like image 159
jezrael Avatar answered Nov 15 '22 11:11

jezrael