• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / pandas nsmallest – Find Smallest Values in Series or Dataframe

pandas nsmallest – Find Smallest Values in Series or Dataframe

January 3, 2022 Leave a Comment

To find the smallest values in a Series or Dataframe column using pandas, the easiest way is to use the pandas nsmallest() function.

df.nsmallest(n,"column")

By default, The pandas nsmallest() function returns the first n smallest rows in the given columns in ascending order.


Finding the smallest values of a column or Series using pandas is easy. We can use the pandas nsmallest() function to find the smallest values of a column or numbers.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [160.20, 123.81, 209.45, 150.35, 102.43, 187.52]})

print(df)
# Output: 
    Name  Weight
0    Jim  160.20
1  Sally  123.81
2    Bob  209.45
3    Sue  150.35
4   Jill  102.43
5  Larry  187.52

To get the 2 smallest values of the numbers in the column “Weight”, we can use the pandas nsmallest() function in the following Python code:

print(df.nsmallest(2,"Weight"))

# Output:
    Name  Weight
4   Jill  102.43
1  Sally  123.81

Please note, you can use the pandas nsmallest() function on a column or Series with numeric values. If we pass “Name” to nsmallest in our example, we will receive an error because the “Name” column is made up of strings.

If you want to find the n largest values, you can use the pandas nlargest() function.

Finding the N Smallest Values in a Column using pandas

The nsmallest() function has a few different options if there are rows with the same values in your Dataframe.

Let’s say our Dataframe from above has changed a little bit and we now have some values which occur multiple times in the column weight:

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [160.20, 160.20, 209.45, 150.35, 187.52, 187.52]})

print(df)
# Output: 
    Name  Weight
0    Jim  160.20
1  Sally  160.20
2    Bob  209.45
3    Sue  150.35
4   Jill  187.52
5  Larry  187.52

By default, the pandas nsmallest() function returns the first occurrence of the nth smallest value.

print(df.nsmallest(2,"Weight"))

# Output:
  Name  Weight
3  Sue  150.35
0  Jim  160.20

In this case, since Jim came before Sally, Jim’s row is returned.

If we want to return the last occurrence, we can pass keep=’last’ to nsmallest():

print(df.nsmallest(2,"Weight", keep='last'))

# Output:
    Name  Weight
3    Sue  150.35
1  Sally  160.20

If we want to keep all rows which contain values in the nth smallest values, we can pass keep=’all’ to nsmallest().

print(df.nsmallest(2,"Weight", keep='all'))

# Output:
    Name  Weight
3    Sue  150.35
0    Jim  160.20
1  Sally  160.20

Find the n Smallest values over Multiple Columns in Dataframe

We can also use the pandas nsmallest() function to find the n smallest values over multiple columns. We just need to pass multiple column names to the function.

Let’s say we have another column on the DataFrame from above:

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [160.20, 160.20, 209.45, 150.35, 187.52, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42] })

print(df)
# Output: 
    Name  Weight  Height
0    Jim  160.20   50.10
1  Sally  160.20   68.94
2    Bob  209.45   71.42
3    Sue  150.35   48.56
4   Jill  187.52   59.37
5  Larry  187.52   63.42

To get the smallest values for both the “Weight” and “Height” columns, we just need to pass both column names in a list like in the following Python code.

print(df.nsmallest(3,["Weight","Height"]))

# Output:
    Name  Weight  Height
3    Sue  150.35   48.56
0    Jim  160.20   50.10
1  Sally  160.20   68.94

This will order the smallest values by the first column, then the second column specified, and so on.

Hopefully this article has been helpful for you to understand how to find the smallest values in a Series or DataFrame using pandas.

Other Articles You'll Also Like:

  • 1.  max function Python – Get Maximum of List or Dictionary with max() Function
  • 2.  Using Python to Replace Backslash in String
  • 3.  Convert False to 0 in Python
  • 4.  Find All Pythagorean Triples in a Range using Python
  • 5.  Get Substring Between Two Characters with Python
  • 6.  How to Check if a Letter is in a String Using Python
  • 7.  Python math.factorial() function – Calculate Factorial of Number
  • 8.  Write Inline If and Inline If Else Statements in Python
  • 9.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 10.  Remove Leading Zeros from String with lstrip() in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy