2017-02-23 37 views
1

我有這樣一個數據幀:如何比較相同數據框的兩列?

match_id inn1 bat bowl runs1 inn2 runs2 is_score_chased 
    1  1  KKR RCB 222 2  82   1 
    2  1  CSK KXIP 240 2  207   1 
    8  1  CSK MI  208 2  202   1 
    9  1  DC RR  214 2  217   1 
    33  1  KKR DC  204 2  181   1 

現在我想改變runs1runs2比較值is_score_chased列中的值。如果runs1> runs2,則該行中的對應值應爲'是'否則應該是。 我試過以下代碼:

for i in (high_scores1): 
    if(high_scores1['runs1']>=high_scores1['runs2']): 
     high_scores1['is_score_chased']='yes' 
    else: 
     high_scores1['is_score_chased']='no' 

但它沒有奏效。我如何更改列中的值?

回答

3

你可以更容易地使用np.where

high_scores1['is_score_chased'] = np.where(high_scores1['runs1']>=high_scores1['runs2'], 
              'yes', 'no') 

通常,如果你發現自己想爲你設置一欄明確地迭代,有像applywhere的抽象,這將是速度更快,更簡潔。

+0

它的工作......非常感謝:) – user517696

+0

@ user517696不客氣! – miradulo

0

您需要引用您正在遍歷數據幀的事實,所以;

for i in (high_scores1): 
    if(high_scores1['runs1'][i]>=high_scores1['runs2'][i]): 
     high_scores1['is_score_chased'][i]='yes' 
    else: 
     high_scores1['is_score_chased'][i]='no' 
0

這是使用apply的好例子。

Here有一個使用兩列應用的例子。然而

def f(x):  
    return 'yes' if x['run1'] > x['run2'] else 'no' 

df['is_score_chased'] = df.apply(f, axis=1) 

,我建議用布爾填寫您的列,這樣就可以使它更簡單

def f(x):  
    return x['run1'] > x['run2'] 

而且還使用lambda表達式這樣:

你可以用這個適應你的問題你把它做成一條線

df['is_score_chased'] = df.apply(lambda x: x['run1'] > x['run2'], axis=1) 
相關問題