如何在 Pandas DataFrame 中分組計數值


要按值計分組,可以使用 Pandas DataFrame 的 groupby()、size() 和 unstack() 方法。首先,建立一個帶有 3 列的 DataFrame -

dataFrame = pd.DataFrame({
   'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'],'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'],'Quantity': [10, 50, 10, 20, 25, 50]})

現在,使用 groupby() 方法按值計分組。要計數,可以使用 size() 和 unstack()。unstack() 提供了一個新的列標籤層 -

dataFrame = dataFrame.groupby(['Product Category', 'Product Name', 'Quantity']).size().unstack(fill_value=0)

示例

以下是完整程式碼 -

import pandas as pd

# create a dataframe with 3 columns
dataFrame = pd.DataFrame({
   'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'],'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'],'Quantity': [10, 50, 10, 20, 25, 50]})

# dataframe
print"Dataframe...\n",dataFrame

# count and unstack
dataFrame = dataFrame.groupby(['Product Category', 'Product Name', 'Quantity']).size().unstack(fill_value=0)

print"\nResultant DataFrame...\n",dataFrame

輸出

這會產生以下輸出 -

Dataframe...
   Product Category   Product Name   Quantity
0         Computer       Keyboard         10
1     Mobile Phone        Charger         50
2      Electronics        SmartTV         10
3      Electronics         Camera         20
4         Computer   Graphic Card         25
5     Mobile Phone       Earphone         50

Resultant DataFrame...
Quantity                          10   20   25   50
Product Category   Product Name
Computer           Graphic Card   0    0    1    0
                   Keyboard       1    0    0    0
Electronics        Camera         0    1    0    0
                   SmartTV        1    0    0    0
Mobile Phone       Charger        0    0    0    1
                   Earphone       0    0    0    1

更新於: 15-9-2021

465 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始動手
廣告
© . All rights reserved.