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
Advertisement
Answer
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