1

I am trying to filter my pandas data frame such that it retains the rows where these rows had one of the top 2 values in any of the data frame's column.

Here is an example:

d = {'col1': [1, 2, 3, 0], 'col2': [4, 6, 5, 2],'col3':[9, 8 , 7, 3], 'col4':[1, 2, 1, 0]}

d= enter image description here

Then I want the output to be:

enter image description here

I have tried using .nlargest and looping through the columns, buts its a mission to merge dataframes. There must be a simpler way, that I am yet to learn. Any help or pointers welcome.

0

2 Answers 2

4

Just try pd.Series.nlargest

newdf=df.apply(pd.Series.nlargest,n=2)
   col1  col2  col3  col4
0   NaN   NaN   9.0   1.0
1   2.0   6.0   8.0   2.0
2   3.0   5.0   NaN   NaN
Sign up to request clarification or add additional context in comments.

Comments

0
apply lambda nlargest on axis=0

  df.apply(lambda x:x.nlargest(2), axis=0)
  
   col1  col2  col3  col4
0   NaN   NaN   9.0   1.0
1   2.0   6.0   8.0   2.0
2   3.0   5.0   NaN   NaN

3 Comments

Hello! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
wwnde, my comment is taken from a template for handling posts in the Low Quality Post review queue that present code without any explanation. It is intended to be helpful to new users who aren't familiar with the qualify standards that SO appreciates. If you have been seeing that comment frequently, then the system may be marking several of your recent posts as being low quality. There really is no need for personal accusations.
Comments are not the place for discussion. In case it is helpful, this is the source of the template: Auto comment on code only answers from Low Quality review. I am sorry that using this template was offensive. If you think that this template can be improved, you may wish to add an answer to the linked meta discussion.

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.