0

I have this example data frame

  winner white_rating black_rating
0 white  1583           1700
1 white  1400           1100
2 black  1200           1000
3 black  1000           2000
4 white  1100           1100

How do I iterate through the data frame stating if white_rating > to black_rating return 0 and if black_rating > white rating return 1 and if white_rating = black_rating return 2

by the way this dataset has 30,000+ rows

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Yigan32
  • 11
  • 4
  • Please provide a [mcve] including sample input, sample _output_, and _code_ for what you've tried based on your own research. Have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Feb 03 '20 at 16:21

3 Answers3

2

Use np.select for multi-condition column:

df['new_col'] = np.select([df['white_rating'] > df['black_rating'], 
                           df['black_rating'] > df['white_rating']],
                          [0,1], default=2)

  winner  white_rating  black_rating  new_col
0  white          1583          1700        1
1  white          1400          1100        0
2  black          1200          1000        0
3  black          1000          2000        1
4  white          1100          1100        2
Erfan
  • 40,971
  • 8
  • 66
  • 78
1

Let's assume your dataframe is df: Try this:

df.loc[df.white_rating > df.black_rating, 'newcol'] = 0
df.loc[df.white_rating < df.black_rating, 'newcol'] = 1
df.loc[df.white_rating == df.black_rating, 'newcol'] = 2

Result:

Out[64]: 
  winner  white_rating  black_rating  newcol
0  white          1583          1700     0.0
1  white          1400          1100     1.0
2  black          1200          1000     1.0
3  black          1000          2000     0.0
4  white          1100          1100     2.0

I made a new column called newcol because you haven't specified where these values should be returned.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
0

I think this method might be helpful,

d=df.apply(lambda x: 0 if  x['white_rating'] > x['black_rating']  else (2 if x['white_rating']==x['black_rating']  else 1) ,axis='columns' )
Mohammed Khalid
  • 155
  • 1
  • 6