Skip to content
Advertisement

Obtaining n number highest values in dataframe also containing strings

I have a dataframe and I would like to find the n highest numbers in each column. There are a variety of methods to do this, but all seem to fail as a result of strings also being in the dataframe. I have tried a multitude of ways to get around this but I am always stumped by the presence of strings.

As some cells contain % a blanket omission of all string type columns wouldn’t work. However, ignoring cells containing A-Z would work.

Example dataframe:

JavaScript

i). Attempt using apply:

JavaScript

ii). a). attempt using a for-loop:

JavaScript

ii). b). I also tried excluding ‘e’ as an experiment to get past the if-statement:

JavaScript

iii). I attempted using numpy as I had see utilised it elsewhere but don’t really grasp the idea:

JavaScript

I could go on but I feel like it would be a waste of text space. Could anyone point me in the right direction?

Example Outcome:

JavaScript

Advertisement

Answer

You can convert the string column to float, then convert it back as str after obtaining the n largest values:

JavaScript

Output for nlargest = 2:

JavaScript
Advertisement