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

时间:2015-12-22 20:52:11

标签: python pandas

我有这样的数据。

    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

我想选择FROM_TIME为NaN或TO_TIME为NaN且SIGNDESC1包含AM|PM

的所有行

这就是我想出来的

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)')

但这样做是行不通的

data[[rows_with_nan_from_time, rows_with_nan_to_time, rows_with_sign_with_am_or_pm]]

1 个答案:

答案 0 :(得分:1)

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

应该有用。