Python – 對 Pandas DataFrame 中的列值進行分組並計算其和


我們考慮汽車銷售記錄,按月分組來計算每月汽車的註冊價格總額。要計算總額,我們使用 sum() 方法。

首先,假設以下是我們包含三列的 Pandas DataFrame −

dataFrame = pd.DataFrame(
   {
      "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"],

      "Date_of_Purchase": [
         pd.Timestamp("2021-06-10"),
         pd.Timestamp("2021-07-11"),
         pd.Timestamp("2021-06-25"),
         pd.Timestamp("2021-06-29"),
         pd.Timestamp("2021-03-20"),
         pd.Timestamp("2021-01-22"),
         pd.Timestamp("2021-01-06"),
         pd.Timestamp("2021-01-04"),
         pd.Timestamp("2021-05-09")
      ],

      "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]
   }
)

在 groupby() 函式中使用 Grouper 選擇 Date_of_Purchase 列。頻次 freq 設定成 "M",按月進行分組,使用 sum() 函式計算總額 −

print"\nGroup Dataframe by month...\n",dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', axis=0, freq='M')).sum()

示例

以下為程式碼 −

import pandas as pd

# dataframe with one of the columns as Date_of_Purchase
dataFrame = pd.DataFrame(
   {
      "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"],

      "Date_of_Purchase": [
         pd.Timestamp("2021-06-10"),
         pd.Timestamp("2021-07-11"),
         pd.Timestamp("2021-06-25"),
         pd.Timestamp("2021-06-29"),
         pd.Timestamp("2021-03-20"),
         pd.Timestamp("2021-01-22"),
         pd.Timestamp("2021-01-06"),
         pd.Timestamp("2021-01-04"),
         pd.Timestamp("2021-05-09")
      ],

      "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]
   }
)

print"DataFrame...\n",dataFrame

# Grouper to select Date_of_Purchase column within groupby function
# calculation the sum month-wise
print"\nGroup Dataframe by month...\n",dataFrame.groupby(pd.Grouper(key='Date_of_Purchase', axis=0, freq='M')).sum()

輸出

將產生以下輸出 −

DataFrame...
        Car   Date_of_Purchase   Reg_Price
0      Audi        2021-06-10        1000
1     Lexus        2021-07-11        1400
2     Tesla        2021-06-25        1100
3  Mercedes        2021-06-29         900
4       BMW        2021-03-20        1700
5    Toyota        2021-01-22        1800
6    Nissan        2021-01-06        1300
7   Bentley        2021-01-04        1150
8   Mustang        2021-05-09        1350

Group Dataframe by month...
                   Reg_Price
Date_of_Purchase
2021-01-31           4250.0
2021-02-28              NaN
2021-03-31           1700.0
2021-04-30              NaN
2021-05-31           1350.0
2021-06-30           3000.0
2021-07-31           1400.0

更新日期:16-9-2021

1K+ 瀏覽

開啟 職業生涯

完成課程以獲得認證

開始學習
廣告