How to compare two columns of the same dataframe?

2020-07-13 10:39发布

I have a dataframe like this:

 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

Now i want to change the values in is_score_chased column by comparing the values in runs1 and runs2 . If runs1>runs2, then the corresponding value in the row should be 'yes' else it should be no. I tried the following code:

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' 

But it didn't work. How do i change the values in the column?

3条回答
你好瞎i
2楼-- · 2020-07-13 11:07

You can more easily use np.where.

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

Typically, if you find yourself trying to iterate explicitly as you were to set a column, there is an abstraction like apply or where which will be both faster and more concise.

查看更多
劳资没心,怎么记你
3楼-- · 2020-07-13 11:07

This is a good case for using apply.

Here there is an example of using apply on two columns.

You can adapt it to your question with this:

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

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

However, I would suggest filling your column with booleans so you can make it more simple

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

And also using lambdas so you make it in one line

df['is_score_chased'] = df.apply(lambda x: x['run1'] > x['run2'], axis=1)
查看更多
欢心
4楼-- · 2020-07-13 11:22

You need to reference the fact that you are iterating through the dataframe, so;

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' 
查看更多
登录 后发表回答