Pandas 如何对 Pandas 中的 Series 按值进行分组

Pandas 如何对 Pandas 中的 Series 按值进行分组

在本文中,我们将介绍如何在 Pandas 中对Series按照值进行分组。分组可以让我们更好地了解我们的数据集,及对其中的某些数据进行计算。

阅读更多:Pandas 教程

准备工作

在开始分组之前,我们需要确保已经导入了 Pandas 库,并且已经创建了我们的数据集。在本文中,我们将使用示例数据集,包含了一个作品列表和它们所属的作者,如下所示:

Book title Author
The Catcher in the Rye J.D. Salinger
To Kill a Mockingbird Harper Lee
1984 George Orwell
The Great Gatsby F. Scott Fitzgerald
Pride and Prejudice Jane Austen

我们将把这个作品列表加载成一个 Pandas Series 对象,然后按照作者进行分组。

分组

首先,我们需要将上述作品列表加载到 Pandas Series 中。你可以通过以下命令将其加载:

import pandas as pd
books = pd.Series(['J.D. Salinger', 'Harper Lee', 'George Orwell', 'F. Scott Fitzgerald', 'Jane Austen'], index=['The Catcher in the Rye', 'To Kill a Mockingbird', '1984', 'The Great Gatsby', 'Pride and Prejudice'])
Python

接下来,我们需要使用 groupby() 方法将数据按作者名称进行分组,如下所示:

grouped_books = books.groupby(books.values)
Python

groupby() 方法会返回一个 Pandas GroupBy 对象,它将分组后的数据存储在已分组对象中。我们可以在 GroupBy 对象上调用一系列方法来对分组后的数据进行计算和分析。

例如,下面的代码将计算作品列表中每个作者的著作数量:

count_books = grouped_books.size()

print(count_books)
Python

输出:

F. Scott Fitzgerald    1
George Orwell          1
Harper Lee             1
J.D. Salinger          1
Jane Austen            1
dtype: int64
Python

其他分组操作

除了上述示例中的 size() 方法之外,还有很多其他的方法可以在分组后的数据上进行计算。以下是一些示例:

sum()

计算分组数据的总和。

sum_books = grouped_books.sum()

print(sum_books)
Python

输出:

F. Scott Fitzgerald          The Great Gatsby
George Orwell                                1984
Harper Lee                  To Kill a Mockingbird
J.D. Salinger               The Catcher in the Rye
Jane Austen                     Pride and Prejudice
dtype: object
Python

mean()

计算分组数据的平均值。

mean_books = grouped_books.mean()

print(mean_books)
Python

输出:

Series([], dtype: float64)
Python

first()

返回分组数据中第一个值。

first_books = grouped_books.first()

print(first_books)
Python

输出:

F. Scott Fitzgerald          The Great Gatsby
George Orwell                                1984
Harper Lee                  To Kill a Mockingbird
J.D. Salinger               The Catcher in the Rye
Jane Austen                     Pride and Prejudice
dtype: object
Python

last()

返回分组数据中最后一个值。

last_books = grouped_books.last()

print(last_books)
Python

输出:

F. Scott Fitzgerald          The Great Gatsby
George Orwell                                1984
Harper Lee                  To Kill a Mockingbird
J.D. Salinger               The Catcher in the Rye
Jane Austen                     Pride and Prejudice
dtype: object
Python

min()

返回分组数据中的最小值。

min_books = grouped_books.min()

print(min_books)
Python

输出:

F. Scott Fitzgerald          The Great Gatsby
George Orwell                                1984
Harper Lee                  To Kill a Mockingbird
J.D. Salinger               The Catcher in the Rye
Jane Austen                     Pride and Prejudice
dtype: object
Python

max()

返回分组数据中的最大值。

max_books = grouped_books.max()

print(max_books)
Python

输出:

F. Scott Fitzgerald          The Great Gatsby
George Orwell                                1984
Harper Lee                  To Kill a Mockingbird
J.D. Salinger               The Catcher in the Rye
Jane Austen                     Pride and Prejudice
dtype: object
Python

count()

返回每个分组中的值的数量。

count_books = grouped_books.count()

print(count_books)
Python

输出:

F. Scott Fitzgerald    1
George Orwell          1
Harper Lee             1
J.D. Salinger          1
Jane Austen            1
dtype: int64
Python

总结

在本文中,我们介绍了如何在 Pandas 中对Series按照值进行分组,并展示了一些经常使用的分组方法。这些方法可以帮助我们更好地理解数据,并对数据进行计算和分析。无论您是在学习数据科学还是在进行实际项目,这些能力都是非常有用的。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册