简体   繁体   English

通过比较一行中的列来过滤Pandas DataFrame

[英]Filter Pandas DataFrame by comparing columns in a row

Each row in my DataFrame has two date columns. 我的DataFrame中的每一行都有两个日期列。 How can I filter out the rows in which 'Date A' is after 'Date B'? 如何过滤出“日期A”在“日期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 应从DataFrame中删除此行,因为'reports_at'列中的日期出现在'as_of'列中的日期之后

You need boolean indexing or query : 您需要boolean indexingquery

1. 1。

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

2. 2。

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

3. 3。

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

4. 4。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM