You can use df.rank
with method set to min
and get the ones that are greater than a number of columns - 2
which is same as (df.shape[1]-n)
to get the top 2. Then astype
to int
-
data = {'First': [1, 2,3],
'Second': [2,1,5],
'Third': [5,3,2]
}
df = pd.DataFrame (data, columns = ['First','Second','Third'])
n = 2 #define top n
(df.rank(1)>(df.shape[1]-n)).astype(int)
First Second Third
0 0 1 1
1 1 0 1
2 1 1 0
Alternate: You can use numpy to solve this as well. The double argsort returns a rank order for each number row-wise. The threshold for top n will be one less in this case since ranking starts from 0 instead. -
new_data = (df.to_numpy().argsort(1).argsort()>(df.shape[1]-n-1)).astype(int)
df2 = pd.DataFrame(new_data, columns = ['First','Second','Third'])
print(df2)
First Second Third
0 0 1 1
1 1 0 1
2 1 1 0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…