sra
sra

Reputation: 167

comparing multiple columns in dataframe (more than 2)

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

Upvotes: 4

Views: 130

Answers (3)

BENY
BENY

Reputation: 323226

Let us try pd.Series.unique with let

data['new'] = data.filter(like='rank').apply(pd.Series.unique,1).str.len().eq(3).astype(int)

Out[45]: 
0    1
1    0
2    1
dtype: int64

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 71689

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

Upvotes: 5

Andrej Kesely
Andrej Kesely

Reputation: 195408

You can use set() and check if the lenght of the set constructed from all the column values == 3:

data["Diff"] = (
    data[["rank", "rank1", "rank2"]]
    .apply(lambda x: len(set(x)) == 3, axis=1)
    .astype(int)
)
print(data)

Prints:

  student  rank  rank1  rank2  Diff
0       a     2      3      4     1
1       b     2      3      2     0
2       c     1      2      3     1

Upvotes: 1

Related Questions