0
0
Pandasdata~5 mins

nlargest() and nsmallest() in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the pandas function nlargest() do?

The nlargest() function returns the top n rows from a DataFrame or Series based on the largest values in a specified column.

Click to reveal answer
beginner
How does nsmallest() differ from nlargest() in pandas?

nsmallest() returns the bottom n rows with the smallest values in a specified column, while nlargest() returns the top n rows with the largest values.

Click to reveal answer
beginner
Can nlargest() and nsmallest() be used on both Series and DataFrames?

Yes, both functions work on pandas Series and DataFrames. For DataFrames, you specify the column to sort by.

Click to reveal answer
beginner
What parameter do you need to provide to nlargest() and nsmallest() to specify how many rows to return?

You provide the parameter n, which is the number of rows you want to get from the top or bottom.

Click to reveal answer
beginner
Example: How to get the 3 largest values from a DataFrame column named 'score'?
df.nlargest(3, 'score')

This returns the 3 rows with the highest 'score' values.

Click to reveal answer
What does df.nlargest(5, 'age') return?
AThe first 5 rows of the DataFrame
BThe 5 rows with the smallest 'age' values
CThe 5 rows with the largest 'age' values
DThe last 5 rows of the DataFrame
Which function returns the rows with the smallest values in a column?
Anlargest()
Bhead()
Csort_values()
Dnsmallest()
Can nlargest() be used on a pandas Series?
AYes, it works on Series and DataFrames
BOnly on DataFrames with numeric columns
COnly on Series with numeric data
DNo, only DataFrames
What parameter do you need to specify in nlargest() to choose the column to sort by?
Acolumns or column name
Bcolumns
Csubset
Dn
If you want the 10 smallest values from a column 'price', which code is correct?
Adf.nsmallest(10, 'price')
BBoth B and C
Cdf.sort_values('price').head(10)
Ddf.nlargest(10, 'price')
Explain how you would use nlargest() to find the top 5 highest scores in a DataFrame.
Think about the parameters needed for nlargest.
You got /4 concepts.
    Describe the difference between nlargest() and nsmallest() in pandas.
    Focus on what values each function returns.
    You got /4 concepts.