2017-08-08 68 views
1

我試圖創建一個if語句,如果支付類別列爲Expense,那麼填充工具類型列爲Legal如果聲明重新映射值

但是,無論支付類別如何,它都會將包含Legal的所有內容都標記爲Legal

test={"Pay Category":["Indemnity","Indemnity","Indemnity","Indemnity","Expense","Expense","Expense","Medical"],"Description of Payment":["Legal","Legal","Legal","Legal","Legal","Legal","Frog","Legal",]} 
test=pd.DataFrame(test) 

test["Tool Type"]="" 
if (test["Pay Category"]=="Medical") is not False: 
test["Tool Type"][test["Description of Payment"].str.contains("Pharmacy|Prescription|RX",case=False)]="Pharmacy" 

if (test["Pay Category"]=='Expense') is not False: 
test["Tool Type"][test["Description of Payment"].str.contains("Legal|Attorney|Court|Defense",case=False)]="Legal" 

我的理解是,if (test["Pay Category"]=='Expense') is not False:是一個布爾值,它要麼是True or False,應該只有在標準「是不假」執行if語句滿足。我錯過了什麼?

布蘭登

+0

搜索支付列的描述,這些關鍵詞在工具類型列中全部映射到Legal。 – Bjc51192

回答

3

我想你需要添加條件,並把它們連與&and):

test["Tool Type"]="" 
m1 = test["Description of Payment"].str.contains("Pharmacy|Prescription|RX",case=False) 
m2 = test["Pay Category"]=="Medical" 

m3 = test["Description of Payment"].str.contains("Legal|Attorney|Court|Defense",case=False) 
m4 = test["Pay Category"]=="Expense" 

test.loc[m1 & m2, "Tool Type"]="Pharmacy" 
test.loc[m3 & m4, "Tool Type"]="Legal" 
print (test) 
    Description of Payment Pay Category Tool Type 
0     Legal Indemnity   
1     Legal Indemnity   
2     Legal Indemnity   
3     Legal Indemnity   
4     Legal  Expense  Legal 
5     Legal  Expense  Legal 
6     Frog  Expense   
7     Legal  Medical   

numpy.where另一種解決方案:

test['Tool Type'] = np.where(m1 & m2, 'Pharmacy', 
        np.where(m3 & m4, 'Legal', '')) 
print (test) 
    Description of Payment Pay Category Tool Type 
0     Legal Indemnity   
1     Legal Indemnity   
2     Legal Indemnity   
3     Legal Indemnity   
4     Legal  Expense  Legal 
5     Legal  Expense  Legal 
6     Frog  Expense   
7     Legal  Medical   

編輯:有很不錯的解決方案unutbu評論用戶numpy.select

test['Tool Type'] = np.select([(m1 & m2), (m3 & m4)], ['Pharmacy', 'Legal'], default='') 
print (test) 
    Description of Payment Pay Category Tool Type 
0     Legal Indemnity   
1     Legal Indemnity   
2     Legal Indemnity   
3     Legal Indemnity   
4     Legal  Expense  Legal 
5     Legal  Expense  Legal 
6     Frog  Expense   
7     Legal  Medical   
+0

偉大的解決方案,你能否澄清我在嘗試中出錯的地方? – Bjc51192

+1

我認爲你使用數組有問題,而不是使用標量。所以如果需要一些條件,使用「True」和「False」值的數組。所以需要['布爾索引'](http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing)和chaning條件。 – jezrael

+0

感謝您的信息。乾杯。 – Bjc51192