简体   繁体   中英

How to filter entire dataframe on boolean condition?

I have two date columns in the same dataframe. I need to filter the dataframe entirely if one set of dates is greater than or equal to the other column.

Example dataframe:

id    updated         compare_date
0     2018-02-10      2018-02-11
1     2018-02-10      2018-02-11
2     2018-02-12      2018-02-11

Code I am trying:

df_1 = df['compare_date'] >= df['updated'])

Result I am getting get back is a boolean list df_1 = [True, True, True]

Result I want

 id    updated         compare_date
 2     2018-02-12      2018-02-11

I have confirmed that dtypes are datetime for the date columns.

Thank you in advance.

It seems like you need to index df1 by the boolean list that you created.

df1 = df.loc[df['compare_date'].ge(df['updated'])]

Example:

>>> df
   id    updated compare_date
0   0 2018-02-10   2018-02-11
1   1 2018-02-10   2018-02-11
2   2 2018-02-12   2018-02-11

>>> df.dtypes
id                       int64
updated         datetime64[ns]
compare_date    datetime64[ns]
dtype: object

>>> df1 = df.loc[df['compare_date'].ge(df['updated'])]

>>> df1
   id    updated compare_date
0   0 2018-02-10   2018-02-11
1   1 2018-02-10   2018-02-11

filter(lambda df: df['compare_date'] >= df['updated'], dataframes)

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