Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the nsmallest method in pandas?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The nsmallest method is used to return the first n rows in ascending order, with the smallest values in columns. The columns that aren’t provided are also returned, but they’re not used for sorting.

Note: Refer to “What is pandas in Python” to learn more about pandas.

Syntax

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

Parameters

  • n: The number of rows to return.

  • columns: The column label to order by. This parameter can be a list specifying multiple columns.

  • keep: This parameter says how to handle duplicate values. There are three possible values for this attribute that are as follows:

    • first: This picks the first occurrences of the duplicates. This is the default value for keep.
    • last: This picks the last occurrences of the duplicates.
    • all: This does not drop any duplicates, even if it involves choosing more than ‘n’ items.

Example 1

import pandas as pd
df= pd.DataFrame({'Name':['Dom', 'Celeste', 'Abhi', 'Gaby', 'Rachel', 'Sam'],'Age': [20,25,30,18,25,20]})
print("Dataframe---")
print(df)
print("\n")
print("Top 2 youngest people:")
print(df.nsmallest(2,'Age'))

Explanation

  • Line 1: We import the pandas module.
  • Line 3: We create a pandas DataFrame called df.
  • Line 6: We print df.
  • Line 9: We print the top two youngest people using the nsmallest method.

The value of the keep argument of the nsmallest method is by default first. Hence, though there were two records with Age as 20, the first occurring record, meaning 0 Dom 20 is taken into consideration.

Example 2

import pandas as pd
df= pd.DataFrame({'Name':['Dom', 'Celeste', 'Abhi', 'Gaby', 'Rachel', 'Sam'],'Age': [20,25,30,18,25,20]})
print("Dataframe---")
print(df)
print("\n")
print("Top 2 youngest people:")
print(df.nsmallest(2,'Age', keep='last'))

Explanation

  • Line 1: We import the pandas module.
  • Line 3: We create a panda DataFrame called df.
  • Line 6: We print df.
  • Line 9: We print the top two people in ascending order of the Age column using the nlargest method with the keep value as last.

Here, the value of the keep argument of the nlargest method is last. Hence, though there were two records with Age as 20, the last occurring record, meaning 5 Sam 20 is taken into consideration.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring