2016-07-26 71 views
1

下面是我的代碼。我試圖解析DataFrame並存儲公司匹配。但是,if語句總是返回true,並且所有內容都保存在數據框current_customers中,即使我的150行中有10行的值大於97.在我的代碼下面是我的數據樣本。複雜if語句返回所有內容true

current_customers = pandas.DataFrame() 
potential_customers = pandas.DataFrame() 
for i in range(0, len(FDA_useful_companies_bing)): 
    if combined_data['match token sort'].iloc[i] or combined_data['match ratio'].iloc[i] or combined_data['match partial ratio'].iloc[i] > 97: 
     current_customers = current_customers.append(combined_data.ix[i,4::]) 
    else: 
     potential_customers = potential_customers.append(combined_data.ix[i,4::]) 

我的數據

Company        City   State  ZIP  FDA Company     FDA City   FDA State FDA ZIP Token sort ratio    match token sort Ratio       match ratio Partial Ratio   match partial ratio 
NOVARTIS       Larchwood  IA   51241 HELGET GAS PRODUCTS INC  Kansas City   MO   64116 AIR PRODUCTS CHEMICALS INC 73    OCEANIC MEDICAL PRODUCTS INC 59    LUCAS INC    78 
BOEHRINGER INGELHEIM VETMEDICA INC Sioux Center IA   51250 SOUTHWEST TECHNOLOGIES INC North Kansas City MO   64116 SOUTHWEST TECHNOLOGIES  100    SOUTHWEST TECHNOLOGIES   92    SOUTHWEST TECHNOLOGIES 100 

編輯的樣本: 此外,如果做到這一點更有效的方式,我很樂意聽到。

+0

我不相信我的是重複的,因爲我的與熊貓數據框如何與if語句一起工作。 – Jstuff

+0

不,問題是標準的Python。 foo或bar或baz> 97'將始終爲foo和bar的非0值返回True。 –

+0

你說得對。我不明白這個錯誤。 – Jstuff

回答

2

IIUC你可以這樣做:

current_customer = combined_data[(combined_data[['match token sort','match ratio','match partial ratio']] > 97).any(axis=1)] 

potential_customer = combined_data[(combined_data[['match token sort','match ratio','match partial ratio']] <= 97).all(axis=1)] 

你嘗試過什麼短路因爲它不是比較所有條款對最後的數值任何非零值將評估爲True如你預期:

if combined_data['match token sort'].iloc[i] or combined_data['match ratio'].iloc[i] or combined_data['match partial ratio'].iloc[i] > 97: 

所以這相當於:

if some_val or another_val or last_val > 95 

所以在這裏,如果some_val是非零或another_val是NON_ZERO那麼整個語句評估爲True

你可以在一個簡化的情況看到:

In [83]: 
x = 1 ​ 
if 5 or x > 95: 
    print('True') 
else: 
    print('False') 

此輸出:

True 

只需進行單一比較:

In [85]: 
if 5 > 95: 
    print('True') 
else: 
    print('False') 

outp UTS:

False 

但每個值與目標值進行比較:

In [87]: 
x=1 
if 5 > 95 or x > 95: 
    print('True') 
else: 
    print('False') 

這個現在打印:

False 

但這裏真正的問題是不循環的一切,你可以通過傳遞感興趣列的列表從您的df中進行選擇,然後可以將整個df與您的標量值進行比較,並使用any(axis=1)生成布爾掩碼並使用此掩碼來返回df ñ你當前的客戶,然後你反轉比較,並使用all(axis=1)找到沒有任何一個列滿足您先前的比較,以過濾潛在客戶的DF的行

+0

哈哈我知道必須有一個更簡單的方法來做到這一點。我很欣賞花時間解釋爲什麼它總是以您的榜樣爲準! – Jstuff

+0

儘管'axis = 1'命令很難理解。 – Jstuff

+1

'axis = 1'參數表示我們希望按行進行比較而不是列方式,這將是'axis = 0',您應該嘗試將其從'1'更改爲'0'以查看差異 – EdChum

0

你的問題是if語句,當你懷疑:

if combined_data['match token sort'].iloc[i] or combined_data['match ratio'].iloc[i] or combined_data['match partial ratio'].iloc[i] > 97: 

你問如果表達 「combined_data [ '匹配令牌排序'] ILOC [I]」 是真實的,它是數字> 0,所以它是根據Python的truthey值。因此,整個表達式返回True。

我會添加括號,使Python是更清楚如何解釋這行代碼:

if (combined_data['match token sort'].iloc[i]) or 
    (combined_data['match ratio'].iloc[i]) or 
    (combined_data['match partial ratio'].iloc[i] > 97): 

Python是單獨評估在括號中的語句,Python considers any non-zero number to be a "truthey" value,因而用作有條件它返回True。這裏有一個更正的表達式:

if (combined_data['match token sort'].iloc[i]) > 97 or 
     (combined_data['match ratio'].iloc[i]) > 97 or 
     (combined_data['match partial ratio'].iloc[i] > 97): 

現在Python將按照您的意圖將每個操作作爲比較操作。