你需要boolean indexing
或query
與布爾由|
(or
)創建&
(and
)或(not
)面具,~
:
df1 = df[(df['EmployeeName'] == 'JOSEPH DRISCOLL') & (df['BasePay'] > 140000)]
或者:
df1 = df.query("EmployeeName == 'JOSEPH DRISCOLL' & BasePay > 140000")
樣品:
df = pd.DataFrame({'EmployeeName':['JOSEPH DRISCOLL','q'],'BasePay':[1000000,10]})
print (df)
BasePay EmployeeName
0 1000000 JOSEPH DRISCOLL
1 10 q
print ((df['EmployeeName'] == 'JOSEPH DRISCOLL') & (df['BasePay'] > 140000))
0 True
1 False
dtype: bool
df1 = df[(df['EmployeeName'] == 'JOSEPH DRISCOLL') & (df['BasePay'] > 140000)]
print (df1)
BasePay EmployeeName
0 1000000 JOSEPH DRISCOLL
df1 = df.query("EmployeeName == 'JOSEPH DRISCOLL' & BasePay > 140000")
print (df1)
BasePay EmployeeName
0 1000000 JOSEPH DRISCOLL
是的。我可以用&做。我不是想在這裏解決一個特定的查詢,而是理解我的理解有什麼問題,我相信df [df [df [should – user3687023