Pandas DataFrame Nsmallest Method

PythonPythonBeginner
Practice Now

Introduction

The Python Pandas DataFrame.nsmallest() method is used to get the first n rows of a DataFrame that are ordered by columns in ascending order. This method returns the first n rows with the smallest values in the specified columns, in ascending order. The columns that are not specified are returned as well, but not used for ordering.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/select_columns -.-> lab-68684{{"`Pandas DataFrame Nsmallest Method`"}} python/lists -.-> lab-68684{{"`Pandas DataFrame Nsmallest Method`"}} python/tuples -.-> lab-68684{{"`Pandas DataFrame Nsmallest Method`"}} python/dictionaries -.-> lab-68684{{"`Pandas DataFrame Nsmallest Method`"}} python/importing_modules -.-> lab-68684{{"`Pandas DataFrame Nsmallest Method`"}} python/numerical_computing -.-> lab-68684{{"`Pandas DataFrame Nsmallest Method`"}} python/data_analysis -.-> lab-68684{{"`Pandas DataFrame Nsmallest Method`"}} python/build_in_functions -.-> lab-68684{{"`Pandas DataFrame Nsmallest Method`"}} end

Import the Pandas library

First, we need to import the Pandas library using the import statement.

import pandas as pd

Create the DataFrame

Next, we need to create a DataFrame to work with. We can use the pd.DataFrame() function to create a DataFrame from a dictionary.

df = pd.DataFrame({'Name':['Chetan','yashas','yuvraj','Pooja','Sindu','Renuka'],'Age':  [20,25,30,18,25,20],'Height': [155,160,175,145,155,165],'Weight': [75,60,75,45,55,65]})

Use the nsmallest() method

Now, we can use the nsmallest() method on the DataFrame to retrieve the first n rows with the smallest values in the specified columns.

df.nsmallest(n, columns, keep='first')

The method takes three parameters:

  • n: The number of rows to return.
  • columns: The label or list of labels that specify the name of the columns to order by.
  • keep: It includes ‘first’, ‘last’, ‘all’, and the default is ‘first’.
    • 'first': prioritize the first occurrence(s) in case of duplicate values.
    • 'last': prioritize the last occurrence(s) in case of duplicate values.
    • 'all': do not drop any duplicates, even if it means selecting more than n items.

Display the results

Finally, we can print the resulting DataFrame to see the first n rows with the smallest values in the specified columns.

print(df.nsmallest(n, columns, keep='first'))

Summary

The DataFrame.nsmallest() method in Python Pandas is a convenient way to retrieve the first n rows with the smallest values in specified columns of a DataFrame. By using this method, we can easily find and display the records that meet our criteria.

Other Python Tutorials you may like