Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - How to Group Pandas DataFrame by Minutes?
Grouping a Pandas DataFrame by minutes is useful for time-series analysis. We can use pd.Grouper() with the freq parameter to specify minute intervals and aggregate data within those time windows.
Creating a Sample DataFrame with Timestamps
Let's create a DataFrame with car sales data that includes timestamps ?
import pandas as pd
# Create DataFrame with timestamp data
dataFrame = pd.DataFrame(
{
"Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"],
"Date_of_Purchase": [
pd.Timestamp("2021-07-28 00:10:00"),
pd.Timestamp("2021-07-28 00:12:00"),
pd.Timestamp("2021-07-28 00:15:00"),
pd.Timestamp("2021-07-28 00:16:00"),
pd.Timestamp("2021-07-28 00:17:00"),
pd.Timestamp("2021-07-28 00:20:00"),
pd.Timestamp("2021-07-28 00:35:00"),
pd.Timestamp("2021-07-28 00:42:00"),
pd.Timestamp("2021-07-28 00:57:00"),
],
"Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]
}
)
print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
Car Date_of_Purchase Reg_Price
0 Audi 2021-07-28 00:10:00 1000
1 Lexus 2021-07-28 00:12:00 1400
2 Tesla 2021-07-28 00:15:00 1100
3 Mercedes 2021-07-28 00:16:00 900
4 BMW 2021-07-28 00:17:00 1700
5 Toyota 2021-07-28 00:20:00 1800
6 Nissan 2021-07-28 00:35:00 1300
7 Bentley 2021-07-28 00:42:00 1150
8 Mustang 2021-07-28 00:57:00 1350
Grouping by 7-Minute Intervals
Use pd.Grouper() to group data into 7-minute intervals and calculate the sum of registration prices ?
import pandas as pd
# Create DataFrame with timestamp data
dataFrame = pd.DataFrame(
{
"Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"],
"Date_of_Purchase": [
pd.Timestamp("2021-07-28 00:10:00"),
pd.Timestamp("2021-07-28 00:12:00"),
pd.Timestamp("2021-07-28 00:15:00"),
pd.Timestamp("2021-07-28 00:16:00"),
pd.Timestamp("2021-07-28 00:17:00"),
pd.Timestamp("2021-07-28 00:20:00"),
pd.Timestamp("2021-07-28 00:35:00"),
pd.Timestamp("2021-07-28 00:42:00"),
pd.Timestamp("2021-07-28 00:57:00"),
],
"Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]
}
)
# Group by 7-minute intervals and sum the registration prices
grouped_data = dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', freq='7min')).sum(numeric_only=True)
print("Grouped by 7-minute intervals:")
print(grouped_data)
Grouped by 7-minute intervals:
Reg_Price
Date_of_Purchase
2021-07-28 00:07:00 2400
2021-07-28 00:14:00 5500
2021-07-28 00:21:00 0
2021-07-28 00:28:00 0
2021-07-28 00:35:00 1300
2021-07-28 00:42:00 1150
2021-07-28 00:49:00 0
2021-07-28 00:56:00 1350
Different Minute Intervals
You can use different minute intervals by changing the freq parameter ?
import pandas as pd
# Same DataFrame as before
dataFrame = pd.DataFrame(
{
"Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"],
"Date_of_Purchase": [
pd.Timestamp("2021-07-28 00:10:00"),
pd.Timestamp("2021-07-28 00:12:00"),
pd.Timestamp("2021-07-28 00:15:00"),
pd.Timestamp("2021-07-28 00:16:00"),
pd.Timestamp("2021-07-28 00:17:00"),
pd.Timestamp("2021-07-28 00:20:00"),
pd.Timestamp("2021-07-28 00:35:00"),
pd.Timestamp("2021-07-28 00:42:00"),
pd.Timestamp("2021-07-28 00:57:00"),
],
"Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]
}
)
# Group by 15-minute intervals
print("Grouped by 15-minute intervals:")
grouped_15min = dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', freq='15min')).sum(numeric_only=True)
print(grouped_15min)
print("\nGrouped by 30-minute intervals:")
grouped_30min = dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', freq='30min')).sum(numeric_only=True)
print(grouped_30min)
Grouped by 15-minute intervals:
Reg_Price
Date_of_Purchase
2021-07-28 00:00:00 7900
2021-07-28 00:15:00 1800
2021-07-28 00:30:00 1300
2021-07-28 00:45:00 2500
Grouped by 30-minute intervals:
Reg_Price
Date_of_Purchase
2021-07-28 00:00:00 9700
2021-07-28 00:30:00 3800
Key Parameters
| Parameter | Description | Example |
|---|---|---|
key |
Column name to group by | 'Date_of_Purchase' |
freq |
Time frequency for grouping | '7min', '15min', '1H' |
axis |
Axis to group along (0=rows, 1=columns) | 0 (default) |
Conclusion
Use pd.Grouper() with the freq parameter to group DataFrame by minute intervals. This is essential for time-series analysis and aggregating data over specific time windows.
Advertisements
