Python - 如何按分钟对 Pandas DataFrame 进行分组?
我们将使用 .pandas DataFrame 对 Pandas DataFrame 进行分组groupby()。使用 grouper 功能选择要使用的列。对于下面显示的汽车销售记录示例,我们将按分钟分组并计算以分钟为间隔的注册价格的总和。
首先,假设以下是我们的三列 Pandas DataFrame。我们已经设置了带有时间戳的 Date_of_Purchase,包括日期和时间 -
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]
}
)
接下来,使用 Grouper 在 groupby 函数中选择 Date_of_Purchase 列。频率设置为 7 分钟,即 7 分钟的间隔分组 -
print"\nGroup Dataframe by 7 minutes...\n",dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', axis=0, freq='7min')).sum()
示例
以下是代码 -
import pandas as pd输出结果# 其中一列为 Date_of_Purchase 的数据框
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"DataFrame...\n",dataFrame
# Grouper 在 groupby 函数中选择 Date_of_Purchase 列
print"\nGroup Dataframe by 7 minutes...\n",dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', axis=0, freq='7min')).sum()
这将产生以下输出 -
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
Group Dataframe by 7 minutes...
Reg_Price
Date_of_Purchase
2021-07-28 00:07:00 2400.0
2021-07-28 00:14:00 5500.0
2021-07-28 00:21:00 NaN
2021-07-28 00:28:00 NaN
2021-07-28 00:35:00 1300.0
2021-07-28 00:42:00 1150.0
2021-07-28 00:49:00 NaN
2021-07-28 00:56:00 1350.0
以上是 Python - 如何按分钟对 Pandas DataFrame 进行分组? 的全部内容, 来源链接: utcz.com/z/360408.html