4

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

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

1 Comment

diff compared to 0 would also only find consecutive duplicates no?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.