2017-05-09 69 views
1

我有一個DF和條件DF [DF [ 'EmployeeName'] == 'JOSEPH德里斯科爾']給出以下輸出enter image description hereUnalignable布爾系列鍵的類型(DF [DF [DF)

而當我輸入這個df,可以[DF [ 'EmployeeName'] == 'JOSEPH德里斯科爾'] [ 'BasePay']> 140000
它給出這樣

24真

名稱的輸出:BasePay,D型:布爾

現在我不能ø像這樣

DF [DF [DF [ 'EmployeeName'] == 'JOSEPH德里斯科爾'] [ 'BasePay']> 140000]

並期望輸出與上述圖像相同??

回答

0

你需要boolean indexingquery與布爾由|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 
+0

是的。我可以用&做。我不是想在這裏解決一個特定的查詢,而是理解我的理解有什麼問題,我相信df [df [df [should – user3687023