Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing multiple columns in dataframe (more than 2)

Tags:

python

pandas

I have a data frame

data = pd.DataFrame({'student': ['a', 'b', 'c'],
                     'rank': [2, 2, 1],
                     'rank1': [3, 3, 2],
                     'rank2': [4, 2, 3]})

my code

import numpy as np

data['Diff'] = np.where((data['rank'] != data['rank1']) &
                        (data['rank1'] != data['rank2']), '1', '0')

requirement all the ranks must be different then 1 else 0 but I am getting b also as 1

like image 380
sra Avatar asked Mar 01 '23 13:03

sra


1 Answers

We can filter the rank like columns, then use nunique along axis=1 to check for the occurrence of N unique values

r = data.filter(like='rank')
data['diff'] = r.nunique(1).eq(r.shape[1]).view('i1')

  student  rank  rank1  rank2  diff
0       a     2      3      4     1
1       b     2      3      2     0
2       c     1      2      3     1
like image 197
Shubham Sharma Avatar answered Mar 15 '23 20:03

Shubham Sharma