Filtering, Sorting, Grouping, and Aggregating Data with Pandas
Course: Python Training
Topic: Filtering, Sorting, Grouping, and Aggregating Data with Pandas
Course Link: Python Training Course – JustAcademy
Demo Link: Register for a Course Demo
Website: JustAcademy Official Website
1. Introduction to Data Manipulation with Pandas
Pandas is a widely used Python library for data cleaning, manipulation, and analysis. It provides the DataFrame structure for working with tabular datasets containing rows and columns. In real-world projects, datasets often contain thousands or millions of records, so it is important to identify relevant rows, arrange records in a meaningful order, divide data into groups, and calculate useful summaries.
Four essential operations in Pandas are filtering, sorting, grouping, and aggregation. These operations help transform raw data into meaningful information for reporting, business intelligence, exploratory data analysis, and machine learning preparation.
- Filtering: Selects records that satisfy one or more conditions.
- Sorting: Arranges records or values in a specified order.
- Grouping: Organizes records into categories based on one or more columns.
- Aggregation: Calculates summary values such as sum, mean, count, minimum, and maximum.
2. Learning Objectives
After completing this topic, learners should be able to:
- Create and inspect a Pandas DataFrame.
- Filter records using comparison and logical operators.
- Apply multiple conditions to select relevant data.
- Filter rows using lists, ranges, missing-value checks, and text conditions.
- Sort data by one or multiple columns.
- Group records using
groupby().
- Calculate aggregate statistics using
sum(), mean(), count(), min(), and max().
- Apply multiple aggregations and create summary reports.
- Understand common errors and follow Pandas best practices.
3. Preparing a Sample Dataset
Throughout these notes, a sales dataset will be used to demonstrate filtering, sorting, grouping, and aggregation. Each row represents a sales transaction, including the product, region, quantity, price, and sales representative.
import pandas as pd
data = {
"Order_ID": [101, 102, 103, 104, 105, 106, 107, 108],
"Product": [
"Laptop", "Phone", "Tablet", "Laptop",
"Phone", "Tablet", "Laptop", "Phone"
],
"Region": [
"North", "South", "East", "North",
"West", "South", "East", "West"
],
"Quantity": [2, 5, 3, 1, 4, 6, 2, 3],
"Unit_Price": [60000, 25000, 18000, 60000, 25000, 18000, 60000, 25000],
"Salesperson": [
"Amit", "Neha", "Rahul", "Amit",
"Priya", "Neha", "Rahul", "Priya"
]
}
df = pd.DataFrame(data)
df["Sales"] = df["Quantity"] * df["Unit_Price"]
print(df)
The Sales column is calculated by multiplying the quantity by the unit price. Creating this column makes it possible to analyze the revenue generated by each transaction.
Inspecting the Dataset
print(df.head())
print(df.shape)
print(df.columns)
print(df.dtypes)
print(df.info())
print(df.describe())
These commands help verify the dataset's structure, column names, data types, and basic numeric statistics before performing analysis.
4. Understanding Data Filtering
Filtering is the process of selecting only those rows that meet specific requirements. For example, a business analyst might want to view sales from the North region, products priced above a certain amount, or orders with quantities greater than three.
Pandas commonly uses Boolean indexing for filtering. A Boolean condition produces True or False for each row. Rows for which the condition is True are selected.
5. Filtering Data Using Comparison Operators
Comparison operators compare column values against specified values.
| Operator |
Meaning |
Example |
== |
Equal to |
df["Region"] == "North" |
!= |
Not equal to |
df["Region"] != "North" |
> |
Greater than |
df["Sales"] > 50000 |
< |
Less than |
df["Quantity"] < 4 |
>= |
Greater than or equal to |
df["Quantity"] >= 3 |
<= |
Less than or equal to |
df["Unit_Price"] <= 25000 |
Example: Filter Sales Above 100,000
high_sales = df[df["Sales"] > 100000]
print(high_sales)
This expression returns only transactions whose calculated sales value is greater than 100,000. The original DataFrame remains unchanged because the filtered result is assigned to a separate variable.
Example: Select Orders from One Region
north_sales = df[df["Region"] == "North"]
print(north_sales)
This filter returns transactions recorded in the North region.
6. Filtering with Multiple Conditions
Business requirements often involve more than one condition. Pandas supports combining Boolean conditions with logical operators.
| Operator |
Purpose |
Equivalent concept |
& |
Both conditions must be true |
AND |
| |
At least one condition must be true |
OR |
~ |
Reverses a Boolean condition |
NOT |
Each comparison should be enclosed in parentheses when combining conditions.
6.1 Using AND
result = df[
(df["Region"] == "North") &
(df["Sales"] > 50000)
]
print(result)
This returns North region transactions with sales greater than 50,000. Both conditions must be satisfied.
6.2 Using OR
result = df[
(df["Region"] == "North") |
(df["Region"] == "South")
]
print(result)
This selects transactions from either the North or South region.
6.3 Using NOT
result = df[~(df["Region"] == "North")]
print(result)
This returns all rows where the region is not North. Missing values require separate consideration because Boolean comparisons involving missing data may not behave like ordinary values.
7. Filtering Using isin()
The isin() method checks whether column values belong to a specified collection. It is especially useful when selecting several categories.
selected_regions = df[
df["Region"].isin(["North", "East", "West"])
]
print(selected_regions)
This is generally easier to read than writing a separate equality comparison for every region.
Excluding Specific Categories
excluded_products = df[
~df["Product"].isin(["Phone", "Tablet"])
]
print(excluded_products)
The tilde operator reverses the membership condition, leaving only products outside the supplied list.
8. Filtering Values Within a Range
The between() method selects values that fall within a specified range. Its endpoints are included by default.
medium_sales = df[
df["Sales"].between(50000, 150000)
]
print(medium_sales)
This example selects transactions with sales from 50,000 through 150,000, including both boundaries.
Using Multiple Numeric Conditions
result = df[
(df["Quantity"] >= 2) &
(df["Quantity"] <= 5)
]
print(result)
Both examples demonstrate range-based filtering. Choose the approach that makes the business rule clearest.
9. Filtering Text Data
Pandas provides the str accessor for applying string operations to an entire text column.
9.1 Finding Text with contains()
laptop_rows = df[
df["Product"].str.contains("Laptop", case=False, na=False)
]
print(laptop_rows)
The case=False argument makes matching case-insensitive, and na=False treats missing text values as non-matches.
9.2 Filtering by Prefix
result = df[
df["Product"].str.startswith("Lap", na=False)
]
print(result)
9.3 Filtering by Suffix
result = df[
df["Product"].str.endswith("e", na=False)
]
print(result)
Text filters are useful for searching product names, customer records, locations, categories, and descriptions.
10. Filtering Missing Values
Missing values can affect comparisons, calculations, and reporting. Use isna() and notna() to identify them.
missing_sales = df[df["Sales"].isna()]
available_sales = df[df["Sales"].notna()]
print(missing_sales)
print(available_sales)
To check missing values across an entire DataFrame:
print(df.isna().sum())
Filtering out missing values is not always the correct solution. Decide whether to remove, replace, or investigate missing records based on the meaning of the data.
11. Understanding Data Sorting
Sorting arranges values or records into a specified order. It helps identify top-performing products, rank sales transactions, find the highest salaries, and organize records for reporting.
Pandas provides two commonly used sorting methods:
sort_values() sorts by values in one or more columns or Series elements.
sort_index() sorts by index labels.
12. Sorting a DataFrame by One Column
The sort_values() method is used to sort rows according to one or more columns.
12.1 Ascending Order
ascending_sales = df.sort_values(
by="Sales",
ascending=True
)
print(ascending_sales)
Ascending order places the smallest sales value first.
12.2 Descending Order
descending_sales = df.sort_values(
by="Sales",
ascending=False
)
print(descending_sales)
Descending order places the largest sales value first, making it useful for identifying high-value transactions.
12.3 Sorting by Quantity
quantity_sorted = df.sort_values(
by="Quantity",
ascending=False
)
print(quantity_sorted)
The result ranks transactions by quantity, from highest to lowest.
13. Sorting by Multiple Columns
When records must be organized using more than one criterion, pass a list of column names to by. A list of Boolean values can specify the sorting direction for each column.
sorted_data = df.sort_values(
by=["Region", "Sales"],
ascending=[True, False]
)
print(sorted_data)
This sorts regions alphabetically and, within each region, sorts transactions from highest to lowest sales.
Practical Example: Rank Products Within Regions
ranked_sales = df.sort_values(
by=["Region", "Quantity"],
ascending=[True, False]
)
print(ranked_sales[["Region", "Product", "Quantity"]])
Multiple-column sorting is useful when a report needs a primary grouping order and a secondary ranking order.
14. Sorting by Index
The sort_index() method arranges rows or columns by their labels.
index_sorted = df.sort_index(ascending=True)
print(index_sorted)
To sort columns by their column labels instead of sorting rows, use axis=1:
columns_sorted = df.sort_index(axis=1)
print(columns_sorted)
Index sorting is useful when row labels or column labels are meaningful and need to be displayed in order.
15. Handling Sorting Options
sort_values() supports additional parameters for handling missing values, choosing sorting algorithms, and deciding how to treat tied values.
sorted_data = df.sort_values(
by="Sales",
ascending=False,
na_position="last"
)
print(sorted_data)
The na_position argument controls where missing values appear: "last" places them at the end, while "first" places them at the beginning.
For a single column, kind can be used to specify a sorting algorithm. If several records have equal sorting values, do not assume their relative order is meaningful unless the chosen method and data provide that guarantee.
16. Introduction to Grouping Data
Grouping divides a dataset into categories based on one or more columns. Each group can then be analyzed independently.
For example, a sales dataset can be grouped by region to determine which region generated the highest revenue. It can also be grouped by product to compare sales performance across products.
Pandas uses the groupby() method to implement the split-apply-combine pattern:
- Split: Divide records into groups based on selected keys.
- Apply: Perform calculations or transformations on each group.
- Combine: Collect the results into a Series or DataFrame.
17. Grouping Data by One Column
To group sales records by region, specify the grouping column inside groupby().
grouped = df.groupby("Region")
print(grouped["Sales"].sum())
This calculates total sales for each region. The result is typically a Series indexed by region.
Displaying Grouped Records
for region, group in df.groupby("Region"):
print("Region:", region)
print(group)
print()
Iterating over a grouped object returns the group key and the corresponding subset of records. This can help when examining groups individually, although vectorized aggregations are generally preferable for straightforward summaries.
18. Grouping by Multiple Columns
A dataset can be grouped using more than one column. This creates groups for combinations of the selected values.
grouped_sales = df.groupby(
["Region", "Product"]
)["Sales"].sum()
print(grouped_sales)
This calculates total sales for each region-product combination. The result normally has a MultiIndex representing the two grouping columns.
Creating a Tabular Summary
summary = df.groupby(
["Region", "Product"],
as_index=False
)["Sales"].sum()
print(summary)
Using as_index=False keeps the grouping keys as ordinary columns in the result, which can be convenient for exports and reports.
19. Basic Aggregation Functions
Aggregation reduces multiple values to a summary statistic. Common functions include the following:
| Function |
Purpose |
Example |
sum() |
Calculates the total |
df["Sales"].sum() |
mean() |
Calculates the arithmetic average |
df["Sales"].mean() |
median() |
Finds the middle value |
df["Sales"].median() |
min() |
Finds the smallest value |
df["Sales"].min() |
max() |
Finds the largest value |
df["Sales"].max() |
count() |
Counts non-missing values |
df["Sales"].count() |
size() |
Counts rows in each group |
df.groupby("Region").size() |
nunique() |
Counts distinct values |
df["Product"].nunique() |
std() |
Calculates standard deviation |
df["Sales"].std() |
var() |
Calculates variance |
df["Sales"].var() |
Be aware that count() excludes missing values, whereas size() counts rows, including rows where some column values are missing.
20. Aggregating Data After Grouping
Aggregation is especially powerful when combined with groupby(). It produces a summary for every group rather than only one statistic for the entire dataset.
20.1 Total Sales by Region
region_sales = df.groupby("Region")["Sales"].sum()
print(region_sales)
20.2 Average Sales by Product
product_average = df.groupby("Product")["Sales"].mean()
print(product_average)
20.3 Number of Transactions by Region
transaction_counts = df.groupby("Region").size()
print(transaction_counts)
20.4 Maximum Sales by Salesperson
top_transaction = df.groupby("Salesperson")["Sales"].max()
print(top_transaction)
These calculations can answer questions about regional performance, product revenue, transaction volume, and individual sales records.
21. Performing Multiple Aggregations with agg()
The agg() method allows several summary functions to be calculated in one operation. It can make reports shorter, clearer, and easier to maintain.
summary = df.groupby("Region")["Sales"].agg(
["sum", "mean", "min", "max", "count"]
)
print(summary)
The resulting DataFrame includes total, average, minimum, maximum, and non-missing sales counts for each region.
Named Aggregation
Named aggregation allows you to define readable output column names and apply different functions to different columns.
summary = df.groupby("Region").agg(
Total_Sales=("Sales", "sum"),
Average_Sales=("Sales", "mean"),
Total_Quantity=("Quantity", "sum"),
Average_Quantity=("Quantity", "mean"),
Transaction_Count=("Order_ID", "count")
)
print(summary)
Named aggregation is useful for producing a report with clearly labeled business metrics.
22. Aggregating Multiple Columns
You can calculate statistics for multiple numeric columns by selecting several columns or applying named aggregation to the grouped DataFrame.
summary = df.groupby("Product")[
["Quantity", "Sales"]
].sum()
print(summary)
This produces the total quantity and total sales for each product.
Different Functions for Different Columns
summary = df.groupby("Product").agg({
"Quantity": ["sum", "mean"],
"Sales": ["sum", "max"],
"Order_ID": "count"
})
print(summary)
When using a dictionary of aggregations, Pandas may produce multi-level column labels. Named aggregation is often preferable when a flat, report-friendly structure is required.
23. Using transform() for Group-Level Calculations
Unlike a typical aggregation, which returns one result per group, transform() returns values aligned to the original rows. This is useful when comparing each transaction against its group's total or average.
Example: Add Regional Average Sales
df["Regional_Average_Sales"] = (
df.groupby("Region")["Sales"].transform("mean")
)
print(df[[
"Region",
"Sales",
"Regional_Average_Sales"
]])
Every row receives the average sales value for its own region. The original number of rows is preserved.
Example: Compare Transactions with Regional Performance
df["Above_Regional_Average"] = (
df["Sales"] > df.groupby("Region")["Sales"].transform("mean")
)
print(df[[
"Region",
"Sales",
"Above_Regional_Average"
]])
This creates a Boolean column indicating whether each transaction exceeds the average sales for its region.
24. Filtering Groups with filter()
The filter() method retains or removes entire groups based on a condition evaluated for each group. It is different from ordinary row filtering.
Example: Retain Regions with at Least Two Transactions
regions_with_multiple_orders = df.groupby("Region").filter(
lambda group: len(group) >= 2
)
print(regions_with_multiple_orders)
This returns every row belonging to a region with at least two transactions. It does not simply return two rows; it retains all rows from groups that satisfy the condition.
Example: Retain Products with High Total Sales
high_revenue_products = df.groupby("Product").filter(
lambda group: group["Sales"].sum() > 100000
)
print(high_revenue_products)
Group filtering is useful when the decision depends on a group-level metric rather than an individual row.
25. Using value_counts() for Category Summaries
The value_counts() method counts the occurrences of distinct values in a Series. It is a convenient way to summarize categorical data.
region_counts = df["Region"].value_counts()
print(region_counts)
By default, the output is ordered by frequency in descending order and excludes missing values. To include missing values, use dropna=False.
Counting Product Categories
product_counts = df["Product"].value_counts(dropna=False)
print(product_counts)
Use value_counts() when you want category frequencies without creating a separate grouped aggregation.
26. Resetting the Index of Grouped Results
Grouping often places grouping columns in the result's index. Use reset_index() to convert index labels back into ordinary columns.
summary = df.groupby("Region")["Sales"].sum()
summary_df = summary.reset_index(name="Total_Sales")
print(summary_df)
Alternatively, specify as_index=False during grouping:
summary_df = df.groupby(
"Region",
as_index=False
)["Sales"].sum()
summary_df = summary_df.rename(
columns={"Sales": "Total_Sales"}
)
print(summary_df)
A flat DataFrame is often easier to export to CSV, share with colleagues, or use in visualization tools.
27. Combining Filtering, Sorting, Grouping, and Aggregation
Real-world analysis often combines all four operations in a single workflow. For example, a company may need to identify regions with high-value sales, calculate revenue by product, and rank the results.
Example: Find High-Value Transactions and Sort Them
high_value = df[df["Sales"] > 50000]
high_value_sorted = high_value.sort_values(
by="Sales",
ascending=False
)
print(high_value_sorted)
Example: Calculate Total Sales by Product and Rank the Products
product_summary = df.groupby(
"Product",
as_index=False
).agg(
Total_Sales=("Sales", "sum"),
Total_Quantity=("Quantity", "sum"),
Order_Count=("Order_ID", "count")
)
product_summary = product_summary.sort_values(
by="Total_Sales",
ascending=False
)
print(product_summary)
Example: Filter Transactions Before Grouping
filtered_df = df[df["Quantity"] >= 2]
summary = filtered_df.groupby(
"Region",
as_index=False
).agg(
Total_Sales=("Sales", "sum"),
Average_Sales=("Sales", "mean"),
Transaction_Count=("Order_ID", "count")
)
summary = summary.sort_values(
by="Total_Sales",
ascending=False
)
print(summary)
This workflow first filters transactions by quantity, then groups the remaining records by region, calculates summary metrics, and sorts the report by total sales.
28. Understanding the Difference Between agg(), transform(), and filter()
| Method |
Main Purpose |
Typical Output |
Example |
agg() |
Calculates summary statistics |
Usually one row per group |
Average sales by region |
transform() |
Calculates group-level values aligned to original rows |
Same number of rows as input for common transformations |
Regional average added to each transaction |
filter() |
Retains groups satisfying a condition |
Subset of original rows |
Regions with at least two orders |
29. Common Errors and Troubleshooting
29.1 Using and/or Instead of & and |
Problem: A combined Boolean filter raises an error or does not behave as expected.
Cause: Python's and and or operators are not suitable for combining element-wise Pandas Series conditions.
Solution: Use & for AND and | for OR, and wrap each comparison in parentheses.
result = df[
(df["Quantity"] > 2) &
(df["Sales"] > 50000)
]
29.2 Missing Parentheses in Filters
Comparison operators and logical operators have different precedence rules. Always use parentheses around individual conditions to make the intended logic explicit.
29.3 KeyError While Sorting or Grouping
Problem: Pandas reports that a specified column does not exist.
Solution: Inspect the available column names and check for spelling, capitalization, and whitespace differences.
print(df.columns.tolist())
df.columns = df.columns.str.strip()
29.4 Incorrect Aggregation Results
Problem: A count or total does not match the expected result.
Possible causes:
- Missing values are excluded by
count().
- Rows have been filtered out before grouping.
- Duplicate records exist in the dataset.
- A column contains strings instead of numeric values.
- The wrong grouping columns were selected.
Solution: Check row counts, data types, missing values, and the business definition of the metric before changing the code.
29.5 Confusing count() with size()
print(df.groupby("Region")["Sales"].count())
print(df.groupby("Region").size())
The first expression counts non-missing sales values. The second counts rows in each group, including rows where the Sales column is missing.
29.6 Unexpected Grouping Index
Grouping may produce an index containing the group labels. Use as_index=False or reset_index() when a flat DataFrame is more convenient.
29.7 Modifying a Filtered DataFrame
When creating a separate dataset for subsequent modification, use copy() to make the intention clear.
high_sales = df[df["Sales"] > 50000].copy()
high_sales["Sales_Category"] = "High"
print(high_sales)
This avoids common chained-assignment issues and makes it explicit that the new column belongs to the separate result.
30. Best Practices for Pandas Data Analysis
- Inspect data types and missing values before performing calculations.
- Use clear column names and consistent naming conventions.
- Write Boolean filters with parentheses around each condition.
- Prefer vectorized filtering and calculations over unnecessary loops.
- Use
sort_values() for value-based ordering and sort_index() for index-based ordering.
- Use
groupby() with explicit grouping columns to make the analysis understandable.
- Use named aggregation to produce readable summary column names.
- Choose between
count() and size() according to the intended metric.
- Use
transform() when group-level results must be added to the original rows.
- Use
filter() when the requirement concerns retaining or removing complete groups.
- Validate totals against the source data before publishing a report.
- Check whether duplicate records should be retained or removed based on the business rules.
- Use meaningful variable names such as
region_summary and top_products.
- Keep intermediate results separate when doing so makes the analysis easier to understand.
- Document assumptions about missing values, filters, and aggregation rules.
31. Practical Exercises
Exercise 1: Basic Filtering
Create a DataFrame containing employee names, departments, and salaries. Filter employees whose salary is greater than 50,000.
Exercise 2: Multiple Conditions
Using the employee dataset, select employees from the IT department whose salaries are at least 60,000.
Exercise 3: Sorting
Create a product dataset with product names, prices, and stock quantities. Sort the products by price from highest to lowest.
Exercise 4: Multiple-Column Sorting
Create a student dataset and sort students by class in ascending order and marks in descending order.
Exercise 5: Grouping
Create a sales DataFrame and calculate total sales for each region.
Exercise 6: Multiple Aggregations
Group employee records by department and calculate average salary, maximum salary, minimum salary, and employee count.
Exercise 7: Group Filtering
Use groupby().filter() to retain departments with at least three employees.
Exercise 8: Complete Analysis
Load a CSV file, filter relevant records, sort them, group by a category, calculate summary statistics, and export the final report.
32. Mini Project: Regional Sales Performance Report
Project objective: Analyze sales transactions to identify high-value orders, compare product performance, and rank regions by revenue.
Project Requirements
- Create a DataFrame containing order ID, product, region, quantity, and unit price.
- Calculate total sales for each transaction.
- Filter transactions whose sales exceed a chosen threshold.
- Sort high-value transactions by sales in descending order.
- Group records by region and calculate total sales and average sales.
- Group records by product and calculate total quantity and revenue.
- Rank regions and products by total sales.
- Export the resulting reports to CSV files.
Sample Project Solution
import pandas as pd
data = {
"Order_ID": [201, 202, 203, 204, 205, 206, 207],
"Product": [
"Laptop", "Phone", "Tablet", "Laptop",
"Phone", "Tablet", "Laptop"
],
"Region": [
"North", "South", "East", "West",
"North", "South", "East"
],
"Quantity": [2, 4, 5, 1, 3, 6, 2],
"Unit_Price": [60000, 25000, 18000, 60000, 25000, 18000, 60000]
}
df = pd.DataFrame(data)
df["Sales"] = df["Quantity"] * df["Unit_Price"]
# Step 1: Filter high-value transactions
high_value_orders = df[
df["Sales"] >= 50000
].copy()
# Step 2: Sort transactions by sales
high_value_orders = high_value_orders.sort_values(
by="Sales",
ascending=False
)
# Step 3: Create regional summary
region_report = df.groupby(
"Region",
as_index=False
).agg(
Total_Sales=("Sales", "sum"),
Average_Sales=("Sales", "mean"),
Total_Quantity=("Quantity", "sum"),
Order_Count=("Order_ID", "count")
)
region_report = region_report.sort_values(
by="Total_Sales",
ascending=False
)
# Step 4: Create product summary
product_report = df.groupby(
"Product",
as_index=False
).agg(
Total_Sales=("Sales", "sum"),
Total_Quantity=("Quantity", "sum"),
Average_Sales=("Sales", "mean"),
Order_Count=("Order_ID", "count")
)
product_report = product_report.sort_values(
by="Total_Sales",
ascending=False
)
# Step 5: Display reports
print("High-Value Orders:")
print(high_value_orders)
print("Regional Performance:")
print(region_report)
print("Product Performance:")
print(product_report)
# Step 6: Export reports
high_value_orders.to_csv(
"high_value_orders.csv",
index=False
)
region_report.to_csv(
"regional_sales_report.csv",
index=False
)
product_report.to_csv(
"product_sales_report.csv",
index=False
)
Expected Learning Outcomes
- Apply filtering, sorting, grouping, and aggregation in one workflow.
- Calculate transaction-level and group-level metrics.
- Generate ranked reports for business decisions.
- Export clean, structured results for further analysis.
33. Interview Questions and Answers
Q1. What is filtering in Pandas?
Answer: Filtering selects rows or values that meet one or more conditions, commonly through Boolean indexing.
Q2. How do you filter rows using multiple conditions?
Answer: Combine Boolean conditions using & for AND or | for OR. Put each comparison in parentheses.
Q3. What is the purpose of isin()?
Answer: isin() checks whether values belong to a specified collection and is useful for filtering several categories.
Q4. What is the difference between sort_values() and sort_index()?
Answer: sort_values() sorts according to data values, while sort_index() sorts according to index labels.
Q5. How can you sort a DataFrame in descending order?
Answer: Use df.sort_values(by="column_name", ascending=False).
Q6. How do you sort by multiple columns?
Answer: Pass a list of columns to by and optionally provide a list of sorting directions to ascending.
Q7. What is groupby() in Pandas?
Answer: groupby() splits records into groups based on selected columns so that calculations can be performed on each group.
Q8. What is aggregation?
Answer: Aggregation summarizes multiple values into statistics such as sum, mean, minimum, maximum, and count.
Q9. What is the difference between count() and size()?
Answer: count() counts non-missing values in a Series or column, whereas size() counts rows in each group, including rows with missing values.
Q10. How can you calculate multiple statistics at once?
Answer: Use agg() or named aggregation with groupby().
Q11. What is transform() used for?
Answer: transform() performs group-based calculations and returns results aligned with the original rows for common transformations.
Q12. What is groupby().filter() used for?
Answer: It retains or removes complete groups based on a condition applied to each group.
Q13. How do you count category occurrences?
Answer: Use value_counts() on a Series.
Q14. How do you find the highest-selling product?
Answer: Group the dataset by product, sum the sales column, and sort the result in descending order.
Q15. How do you retain groups with at least five records?
Answer: Use df.groupby("Category").filter(lambda group: len(group) >= 5).
Q16. Why should Boolean conditions be enclosed in parentheses?
Answer: Parentheses make the intended evaluation order explicit and help avoid errors caused by operator precedence.
Q17. How do you produce a flat grouped report?
Answer: Use groupby(..., as_index=False) or reset the index after aggregation.
Q18. How can you compare each row with its group's average?
Answer: Use groupby("GroupColumn")["ValueColumn"].transform("mean") and compare the resulting Series with the original values.
34. Quick Revision Checklist
- Understand Boolean indexing and comparison operators.
- Combine multiple conditions with
&, |, and ~.
- Filter categories using
isin() and ranges using between().
- Use string methods for text filtering.
- Use
sort_values() and sort_index() appropriately.
- Sort by multiple columns with separate ascending or descending settings.
- Group records with
groupby().
- Calculate totals, averages, counts, minimums, and maximums.
- Apply multiple aggregations using
agg().
- Understand the difference between
agg(), transform(), and filter().
- Distinguish between
count() and size().
- Combine filtering, sorting, grouping, and aggregation into complete reports.
- Validate the results and export reports when required.
35. Conclusion
Filtering, sorting, grouping, and aggregating are fundamental Pandas techniques for transforming raw datasets into meaningful insights. Filtering isolates relevant records, sorting ranks information, grouping organizes records into categories, and aggregation produces useful summary statistics. When these techniques are combined, Python developers and data analysts can create repeatable workflows for sales reports, employee analysis, student performance, financial summaries, and many other real-world applications.
Continue practicing these operations with different datasets and business questions. To explore Python training opportunities, visit the JustAcademy Python Training Course, explore the JustAcademy website, or register for a course demo.