
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas groupby() Method
The Pandasgroupby() method in Python is a powerful tool for data aggregation and analysis. It splits the data into groups, applies a function to each group, and combines the results. This method is essential for data analysis tasks like aggregation, transformations and filtration.
The Pandasgroupby() method can be used on both PandasSeries andDataFrame objects, including those with hierarchical indexes. This method is designed to −
Split data into groups based on specified criteria.
Apply a function to each group independently.
Combine the results into a structured format.
Syntax
Following is the syntax of the Python Pandas groupby() method
Series.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, observed=<no_default>, dropna=True)
Parameters
The Python Pandas groupby() method accepts the below parameters −
by: Used to define how to group data. It can be a function, label, Series, or list of labels.
axis: Determines grouping by rows (0) or columns (1).
level: Groups by specific levels of a MultiIndex.
as_index: If True, group labels are used as the index in the result. If False, returns the result with the original index.
sort: Sort group keys (default is True).
group_keys: Adds group keys to the index when applying functions. OR Adds group keys to the result if True.
observed: If True, shows only observed categories when grouping the categorical data.
dropna: If True, excludes NA values from group keys.
Return Value
The Pandasgroupby() method returns a special object depending on the input type. This object is eitherpandas.api.typing.DataFrameGroupBy orpandas.api.typing.SeriesGroupBy, representing grouped data for further operations.
Example: Grouping a Series by Index Labels
This example demonstrates the basic functionality of theSeries.groupby() method by grouping a Pandas Series using index labels.
import pandas as pds = pd.Series([1000, 1400, 1000, 900, 1700], index=['BMW', 'Audi', 'Mercedes', 'Audi', 'BMW'], name='Car')# Display the Input Seriesprint("Original Series:")print(s)# Grouping the Series by Index Labelsresult = s.groupby(level=0).sum()print("\nSeries after Grouping:")print(result)When we run above program, it produces following result −
Original Series:BMW 1000Audi 1400Mercedes 1000Audi 900BMW 1700Name: Car, dtype: int64Series after Grouping:Audi 2300BMW 2700Mercedes 1000Name: Car, dtype: int64
Example: Grouping a DataFrame Column
The following example demonstrates using the Pandasgroupby() method for grouping the DataFrame column.
import pandas as pd# Create a DataFrame df = pd.DataFrame({'Car':['BMW', 'Audi', 'Mercedes', 'Audi', 'BMW'], 'Price':[1000, 1400, 1000, 900, 1700]})# Display the Input DataFrameprint("Input DataFrame:")print(df)# Grouping a DataFrame Columnresult = df.groupby("Car").mean()print("\nDataFrame after Grouping Based on a Column:")print(result)While executing the above code we get the following output −
Input DataFrame:
| Car | Price | |
|---|---|---|
| 0 | BMW | 1000 |
| 1 | Audi | 1400 |
| 2 | Mercedes | 1000 |
| 3 | Audi | 900 |
| 4 | BMW | 1700 |
| Price | |
|---|---|
| Car | |
| Audi | 1150.0 |
| BMW | 1350.0 |
| Mercedes | 1000.0 |
Example: Grouping while Handling Missing Values
Handling missing values is a easy task while grouping the Pandas objects using thedropna parameter. The following example sets thedropna=False for including NA values as a separate group.
import pandas as pdimport numpy as np# Create a DataFrame df = pd.DataFrame({'Car':['BMW', 'Audi', np.nan, 'Audi', 'BMW'], 'Price':[1000, 1400, 1000, 900, 1700]})# Display the Input DataFrameprint("Input DataFrame:")print(df)# Including NA as a separate groupresult = df.groupby("Car", dropna=False).sum()print("\nDataFrame after Grouping:")print(result)Following is an output of the above code −
Input DataFrame:
| Car | Price | |
|---|---|---|
| 0 | BMW | 1000 |
| 1 | Audi | 1400 |
| 2 | NaN | 1000 |
| 3 | Audi | 900 |
| 4 | BMW | 1700 |
| Price | |
|---|---|
| Car | |
| Audi | 2300 |
| BMW | 2700 |
| Nan | 1000 |
Example: Grouping by Multiple Columns
This example demonstrates grouping a Pandas DataFrame by multiple columns.
import pandas as pdimport numpy as np# Create a DataFrame df = pd.DataFrame({'Car':['BMW', 'Audi', np.nan, 'Audi', 'BMW'], 'Price':[1000, 1400, 1000, 900, 1700],'color': ['white', 'black', 'red', 'red', 'white']})# Display the Input DataFrameprint("Input DataFrame:")print(df)# Grouping a DataFrame by multiple columns result = df.groupby(["Car", "color"], dropna=False).sum()print("\nDataFrame after Grouping Based on Multiple Column:")print(result)When we run above program, it produces following result −
Input DataFrame:
| Car | Price | color | |
|---|---|---|---|
| 0 | BMW | 1000 | white |
| 1 | Audi | 1400 | black |
| 2 | NaN | 1000 | red |
| 3 | Audi | 900 | red |
| 4 | BMW | 1700 | white |
| Price | ||
|---|---|---|
| Car | color | |
| Audi | black | 1400 |
| red | 900 | |
| BMW | white | 2700 |
| NaN | red | 1000 |
Example: Grouping with Hierarchical Indexes
Grouping of a hierarchical index can be done by using thelevel parameter of thegroupby() method. following example demonstrates the same.
import pandas as pdimport numpy as np# Create a DataFrame data = [['BMW', 'BMW', 'Audi', 'Audi'], ['white', 'black', 'black', 'white']]# Create a MultiIndex objectindex = pd.MultiIndex.from_arrays(data, names=("car", "color"))# Creating a MultiIndexed Seriesdf = pd.DataFrame({'Price': [1000, 1400, 1000, 900]}, index=index)# Display the input MultiIndexed DataFrameprint("Input MultiIndexed DataFrame:\n")print(df)# Grouping MultiIndexed by level nameresult = df.groupby("car").sum()print("\nMultiIndexed DataFrame after Grouping:")print(result)Following is an output of the above code −
Input MultiIndexed DataFrame:
| Price | ||
|---|---|---|
| car | color | |
| BMW | white | 1000 |
| black | 1400 | |
| Audi | black | 1000 |
| white | 900 |
| Price | |
|---|---|
| car | |
| Audi | 1900 |
| BMW | 2400 |