歡迎您光臨本站 註冊首頁

pandas 像SQL一樣使用WHERE IN查詢條件說明

←手機掃碼閱讀     lousu-xi @ 2020-06-12 , reply:0

in

newDropList = [9,10,11,12,22,50,51,60,61]
 newDB = newDB[newDB['groupId'].isin(newDropList)]

直接查詢表中groupId列,值為newDropList的記錄

not in

newDropList = [9,10,11,12,22,50,51,60,61]
 newDB = newDB[-newDB['groupId'].isin(newDropList)]

直接加一個" - " 號即可

補充知識:pandas條件組合篩選和按範圍篩選

1、從記錄中選出所有fault_code列的值在fault_list= [487, 479, 500, 505]這個範圍內的記錄

record2=record[record['FAULT_CODE'].isin(fault_list)]

要用.isin 而不能用in,用 in以後選出來的值都是True 和False,然後報錯:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any()

2、選出所有WTGS_CODE=20004013的記錄

set=20004013

record= record[record['WTGS_CODE'] == set]

3、其次,從記錄中選出所有滿足set條件且fault_code列的值在fault_list= [487, 479, 500, 505]這個範圍內的記錄

record_this_month=record[(record['WTGS_CODE']==set)&(record['FAULT_CODE'].isin(fault_list))]

(1)多個條件篩選的時候每個條件都必須加括號。

(2)判斷值是否在某一個範圍內進行篩選的時候需要使用DataFrame.isin()的isin()函數,而不能使用in。


[lousu-xi ] pandas 像SQL一樣使用WHERE IN查詢條件說明已經有231次圍觀

http://coctec.com/docs/python/shhow-post-238231.html