简体   繁体   English

使用 boolean 逻辑基于多个条件过滤 dataframe

[英]Filtering dataframe based on multiple conditions using boolean logic

I'm trying to filter a dataframe based on the following conditions, if end_date is Nan OR if the end_date is greater than the current date.我正在尝试根据以下条件过滤 dataframe 如果end_dateNan或者end_date大于当前日期。 How exactly can I do this?我该怎么做? I'm using the following code, I am aware the | and &我正在使用以下代码,我知道| and & | and & operators are series operators. | and &运算符是系列运算符。

end_series = ((~df.end_date.notna()) | (df.end_date > datetime.datetime.utcnow().date()))
new_df = df[(df.column1 > 0)  &  end_series].copy()

You can do:你可以做:

current_date = pd.to_datetime("today").date()
new_df = df.query("end_date.isna() or (end_date > @current_date) and column1 > 0")

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

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