Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Python - Gom nhóm DataFrame Pandas theo phút (Group by Minutes)


Sử dụng groupby() với pd.Grouper trên cột timestamp để gom nhóm dữ liệu theo khoảng thời gian (ví dụ 7 phút) và tính tổng (hoặc phép toán khác) cho cột số.

Tạo DataFrame có cột Date_of_Purchase dạng Timestamp:

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]
})

Gom nhóm theo 7 phút:

dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', axis=0, freq='7min')).sum()

Ví dụ hoàn chỉnh

import pandas as pd
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)
print("\nGroup Dataframe by 7 minutes...\n", dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', axis=0, freq='7min')).sum())

Kết quả

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