Pandas groupby 计数

Suraj Joshi 2023年1月30日
  1. 使用 Series.value_counts() 方法计算唯一行值的数值
  2. 使用 DataFrame.groupby() 函数对 DataFrame 组的值进行计数
  3. 使用 pandas.DataFrame.agg() 方法获取每组的多个统计值
Pandas groupby 计数

本教程解释了如何从 DataFrame.groupby() 方法中获取像 countsummax 等派生组的统计数据。

我们将用上面例子中所示的 automobile_data_df 来解释这些概念。DataFrame 由员工和他们使用的汽车和自行车品牌组成。

import pandas as pd

automobile_data = [
    ["Chevrolet", "Yamaha"],
    ["Toyota", "Ducati"],
    ["Volkswagen", "BMW"],
    ["Cherlovet", "Kawasaki"],
    ["Toyota", "Ducati"],
    ["Toyota", "BMW"],
    ["Volkswagen", "BMW"],
]

column_names = ["Car Brand", "Motorbike Brand"]
index_names = ["Steven", "Robert", "Jimmy", "Mike", "Phoenix", "Scott", "John"]
automobile_data_df = pd.DataFrame(
    automobile_data, index=index_names, columns=column_names
)
print(automobile_data_df)

输出:

          Car Brand Motorbike Brand
Steven    Chevrolet          Yamaha
Robert       Toyota          Ducati
Jimmy    Volkswagen             BMW
Mike      Cherlovet        Kawasaki
Phoenix      Toyota          Ducati
Scott        Toyota             BMW
John     Volkswagen             BMW

使用 Series.value_counts() 方法计算唯一行值的数值

如果我们想要一个 DataFrame 中单列的每个唯一值的计数,我们可以使用 Series.value_counts() 方法。

import pandas as pd

automobile_data = [
    ["Chevrolet", "Yamaha"],
    ["Toyota", "Ducati"],
    ["Volkswagen", "BMW"],
    ["Cherlovet", "Kawasaki"],
    ["Toyota", "Ducati"],
    ["Toyota", "BMW"],
    ["Volkswagen", "BMW"],
]

column_names = ["Car Brand", "Motorbike Brand"]
index_names = ["Steven", "Robert", "Jimmy", "Mike", "Phoenix", "Scott", "John"]
automobile_data_df = pd.DataFrame(
    automobile_data, index=index_names, columns=column_names
)

print(automobile_data_df["Car Brand"].value_counts())

输出:

Toyota        3
Volkswagen    2
Cherlovet     1
Chevrolet     1
Name: Car Brand, dtype: int64

它将打印 automobile_data_dfCar Brand 列中所有的唯一值及其计数。该方法只适用于我们希望一次只看到一列的数值计数。

使用 DataFrame.groupby() 函数对 DataFrame 组的值进行计数

我们使用 DataFrame.groupby() 函数将一个 DataFrame 分割成多个组,然后使用 size() 方法计算每个创建组的值。

import pandas as pd

automobile_data = [
    ["Chevrolet", "Yamaha"],
    ["Toyota", "Ducati"],
    ["Volkswagen", "BMW"],
    ["Cherlovet", "Kawasaki"],
    ["Toyota", "Ducati"],
    ["Toyota", "BMW"],
    ["Volkswagen", "BMW"],
]

column_names = ["Car Brand", "Motorbike Brand"]
index_names = ["Steven", "Robert", "Jimmy", "Mike", "Phoenix", "Scott", "John"]
automobile_data_df = pd.DataFrame(
    automobile_data, index=index_names, columns=column_names
)

group = automobile_data_df.groupby(["Car Brand", "Motorbike Brand"])
print(group.size().reset_index(name="counts"))

输出:

    Car Brand Motorbike Brand  counts
0   Cherlovet        Kawasaki       1
1   Chevrolet          Yamaha       1
2      Toyota             BMW       1
3      Toyota          Ducati       2
4  Volkswagen             BMW       2

它打印一个 DatFrame,其中包含了所有来自 automobile_data_dfCar BrandMotorbike Brand 列的值的唯一组合,以及这些组合在 automobile_data_df DataFrame 中出现的次数。

groupby() 方法将 automobile_data_df 分割成组。Car BrandMotorbike Brand 两列数值相同的行将被放置在同一组中。size() 方法会给出每组的值的数量,最后我们根据每组的值的数量生成 DataFrame。

使用 pandas.DataFrame.agg() 方法获取每组的多个统计值

import pandas as pd

automobile_data = [
    ["Chevrolet", "Yamaha", 4000],
    ["Toyota", "Ducati", 5000],
    ["Volkswagen", "BMW", 4500],
    ["Cherlovet", "Kawasaki", 3800],
    ["Toyota", "Ducati", 4500],
    ["Toyota", "BMW", 5000],
    ["Volkswagen", "BMW", 6000],
]

column_names = ["Car Brand", "Motorbike Brand", "Monthly Salary"]
index_names = ["Steven", "Robert", "Jimmy", "Mike", "Phoenix", "Scott", "John"]
automobile_data_df = pd.DataFrame(
    automobile_data, index=index_names, columns=column_names
)

group = automobile_data_df.groupby(["Car Brand", "Motorbike Brand"])

print(group.agg({"Monthly Salary": [min, max, sum]}))

输出:

                           Monthly Salary             
                                      min   max    sum
Car Brand  Motorbike Brand                            
Cherlovet  Kawasaki                  3800  3800   3800
Chevrolet  Yamaha                    4000  4000   4000
Toyota     BMW                       5000  5000   5000
           Ducati                    4500  5000   9500
Volkswagen BMW                       4500  6000  10500

它显示每个生成组的 Monthly Salary 值的最小值、最大值和总和。

作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相关文章 - Pandas Groupby