简体   繁体   English

如何使用Pandas中许多列的条件从数据框中选择行?

[英]How to select rows from a data frame using a condition from many columns in Pandas?

I have data like this. 我有这样的数据。

    longtitude  latitude    OBJECTID    SG_KEY_BOR  SG_ORDER_N  SG_SEQNO_N  SG_MUTCD_C  SR_DIST SG_SIGN_FC  SG_ARROW_D  x   y   SIGNDESC1   FROM_TIME   TO_TIME
1   -73.922335  40.836256   11919718    B   P-132428    4   SP-672G 45  NaN NaN 1005740.867110  243957.356623   EXCEPT  NaN NaN
2   -73.922335  40.836256   11919719    B   P-132428    5   SP-579G 45  NaN NaN 1005740.867110  243957.356623   AMBULETTE   NaN NaN
4   -73.922330  40.836352   11919721    B   P-132428    7   SP-672G 80  NaN NaN 1005742.328390  243992.461212   EXCEPT  NaN NaN
5   -73.922330  40.836352   11919722    B   P-132428    8   SP-579G 80  NaN NaN 1005742.328390  243992.461212   AMBULETTE   NaN NaN
9   -73.922317  40.836592   11919726    B   P-132428    12  R7-66A  167 NaN N   1005745.981600  244079.722520   NO PARKING PASSENGER LOADING ZONE W/ SINGLE ARROW   NaN NaN

I'd like to select all the rows that either FROM_TIME is NaN or TO_TIME is NaN and SIGNDESC1 contains either AM|PM 我想选择FROM_TIME为NaN或TO_TIME为NaN且SIGNDESC1包含AM|PM

This is what I come up with 这就是我想出的

rows_with_nan_from_time = data['FROM_TIME'].isnull()
rows_with_nan_to_time = data['TO_TIME'].isnull()
rows_with_sign_with_am_or_pm = data['SIGNDESC1'].str.contains('(?:[AP]M)')

But doing this wouldn't work 但是这样做是行不通的

data[[rows_with_nan_from_time, rows_with_nan_to_time, rows_with_sign_with_am_or_pm]]

data[(data['FROM_TIME'].isnull() | data['TO_TIME'].isnull()) | (data['SIGNDESC1'].str.contains('(AM|PM)'))]

应该管用。

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

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