0
0
Pandasdata~30 mins

nlargest() and nsmallest() in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Find Top and Bottom Sales Using nlargest() and nsmallest()
📖 Scenario: You work in a store and have a list of products with their sales numbers. You want to find which products sold the most and which sold the least.
🎯 Goal: Use pandas nlargest() and nsmallest() functions to find the top 3 and bottom 2 selling products from the sales data.
📋 What You'll Learn
Create a pandas DataFrame with product names and their sales numbers.
Create a variable to store the number of top and bottom products to find.
Use nlargest() to find the top 3 selling products.
Use nsmallest() to find the bottom 2 selling products.
Print the results clearly.
💡 Why This Matters
🌍 Real World
Stores and businesses often need to quickly find their best and worst selling products to make decisions about stock and promotions.
💼 Career
Data analysts and data scientists use functions like <code>nlargest()</code> and <code>nsmallest()</code> to summarize and report key insights from sales data.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales_data with two columns: 'Product' and 'Sales'. Use these exact entries: 'Apples' with sales 150, 'Bananas' with sales 120, 'Cherries' with sales 90, 'Dates' with sales 200, and 'Elderberries' with sales 50.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary containing the product names and sales numbers.

2
Set the number of top and bottom products
Create two variables: top_n and bottom_n. Set top_n to 3 and bottom_n to 2.
Pandas
Need a hint?

Just create two variables and assign the numbers 3 and 2 to them.

3
Find top and bottom selling products
Use nlargest() on sales_data to find the top top_n products by 'Sales' and store it in top_sellers. Use nsmallest() on sales_data to find the bottom bottom_n products by 'Sales' and store it in bottom_sellers.
Pandas
Need a hint?

Use sales_data.nlargest(top_n, 'Sales') and sales_data.nsmallest(bottom_n, 'Sales').

4
Print the top and bottom sellers
Print the top_sellers DataFrame with the label 'Top Sellers:'. Then print the bottom_sellers DataFrame with the label 'Bottom Sellers:'.
Pandas
Need a hint?

Use print() to show the labels and DataFrames.