Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

Find top n values in row of a dataframe (Python)

i want to find the top n values in a row of a dataframe.

Practical example:

data = {'First':  [1, 2,3],
        'Second': [2,1,5],
         'Third': [5,1,2]
        }
df = pd.DataFrame (data, columns = ['First','Second','Third'])

 First Second Third
0   1   2   5
1   2   1   3
2   3   5   2

I want to iterate through each row and select the top n values. In this example the top 2 and replace the values with 1 and all others with 0.

So my desired output would look like:

 First Second Third
0   0   1   1
1   1   0   1
2   1   1   0
question from:https://stackoverflow.com/questions/65937496/find-top-n-values-in-row-of-a-dataframe-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...