Articles

nsmallest() & nlargest() function in pandas with examples

nsmallest() & nlargest() function in pandas with examples


In this pandas tutorial, we will discuss about:

  • using nsmallest in pandas,
  • nsmallest pandas example,
  • nlargest in pandas,
  • nlargest pandas example,

Before going ahead with nsmallest nlargest pandas functions lets see about DataFrame.

 

DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe , The data can be a dictionary that stores list of values with specified key

 

Example: Create DataFrame

In this example, we will create dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: DataFrame is created below

      college_id       college_name college_address Total Staff
one        c-001  vignan university          guntur        1200
two        c-021               vvit          guntur        3422
three      c-002           RVR - JC          guntur        5644
four       c-004  Andhra University          guntur         670

Now lets learn to use function nsmallest nlargest pandas.


Using nsmallest in pandas

This nsmallest function in pandas is used to get the rows from a column with smallest values.

Syntax:

dataframe.nsmallest(n, "column")
(or)
dataframe['column'].nsmallest(n)

Note - The first syntax returns the entire dataframe and second syntax retunr only the specified column.

where,

1. dataframe is the input dataframe

2. n is the first parameter used to get n smallest values/rows in a column.

3. column represents the column name.

 

Example 1nsmallest pandas example

In this nsmallest pandas example, we will get the smallest values from Total Staff column.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the first 2 smallest values based on Total Staff column
print(data.nsmallest(2, "Total Staff"))

print()

#display the first 3 smallest values based on Total Staff column
print(data.nsmallest(3, "Total Staff"))

Outputnsmallest pandas example result is shown below

     college_id       college_name college_address  Total Staff
four      c-004  Andhra University          guntur          670
one       c-001  vignan university          guntur         1200

     college_id       college_name college_address  Total Staff
four      c-004  Andhra University          guntur          670
one       c-001  vignan university          guntur         1200
two       c-021               vvit          guntur         3422

Lets see one more example on nsmallest in pandas.

 

Example 2: nsmallest pandas example

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the first 2 smallest values based on Total Staff column
print(data['Total Staff'].nsmallest(2))

print()

#display the first 3 smallest values based on Total Staff column
print(data['Total Staff'].nsmallest(3))

Outputnsmallest pandas example result

four     670
one     1200
Name: Total Staff, dtype: int64

four     670
one     1200
two     3422
Name: Total Staff, dtype: int64

We have already seen nsmallest pandas example. Now lets see 


nlargest in pandas

This function is used to get the  rows from a column with largest values.

Syntax:

dataframe.nlargest(n, "column")
(or)
dataframe['column'].nlargest(n)

Note - The first syntax returns the entire dataframe and second syntax retunr only the specified column.

where,

1. dataframe is the input dataframe

2. n is the first parameter used to get n smallest values/rows in a column.

3. column represents the column name.

 

Example 1nlargest pandas example

In this nlargest pandas example, we will get the largest values from Total Staff column.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the first 2 largest values based on Total Staff column
print(data.nlargest(2, "Total Staff"))

print()

#display the first 3 largest values based on Total Staff column
print(data.nlargest(3, "Total Staff"))

Outputnlargest pandas example result

      college_id college_name college_address  Total Staff
three      c-002     RVR - JC          guntur         5644
two        c-021         vvit          guntur         3422

      college_id       college_name college_address  Total Staff
three      c-002           RVR - JC          guntur         5644
two        c-021               vvit          guntur         3422
one        c-001  vignan university          guntur         1200

Lets see one more example on nlargest in pandas.

 

Example 2nlargest pandas example

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the first 2 largest values based on Total Staff column
print(data['Total Staff'].nlargest(2))

print()

#display the first 3 largest values based on Total Staff column
print(data['Total Staff'].nlargest(3))

Outputnlargest pandas example result

three    5644
two      3422
Name: Total Staff, dtype: int64

three    5644
two      3422
one      1200
Name: Total Staff, dtype: int64

This completes our tutorial on nsmallest pandas example and nlargest pandas example.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 23,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!