Pandas 如何在系列中显示最常出现的值

Pandas 如何在系列中显示最常出现的值

在本教程中,我们将学习如何使用Python显示Pandas系列中最常出现的值。在本教程中,我们将使用Pandas库。

系列是Pandas中类似于Excel表或SQL表中的列的数据结构。它是一个一维标记数据结构,可以保存不同的数据类型,如整数、浮点数、字符串等。

最常出现的值是在系列中出现最频繁的值。在数学术语中,它是数据的众数。

方法1

在Pandas系列中显示最常出现的值的一种方法是使用value_counts()方法。它返回一个系列,其中包含按降序排序的每个唯一值的计数。它将原始值作为其索引。

语法

要使用value_counts()方法显示图像,您需要遵循以下语法−

counts = s.value_counts()
print(counts.index[0])

我们使用series ‘s’的 ‘value_counts()’ 方法来找到最频繁的值。 ‘counts.index[0]’ 将返回counts中第一个值的索引。然后我们将使用print()函数将其打印出来。

示例

在这个示例中,我们使用Pandas库的Series()函数来创建一个Pandas系列。我们将一个随机整数列表传递给Series()函数,它返回一个系列,我们将其存储在变量’s’中。然后我们将使用’counts.index[0]’来获取系列中最频繁的值。

然后我们将使用print()函数显示最频繁的值。

import pandas as pd

# create a Series with some repeated values
s = pd.Series([1, 2, 2, 3, 3, 3, 4])

# use value_counts() to get the counts of each unique value
counts = s.value_counts()

# print the most frequent value
print(counts.index[0])

输出

3

示例

在这个示例中,我们有一个名为’names’的人名列表。我们首先使用 pd.Series() 函数将列表’names’转换为一个Pandas Series数据结构。这个系列被称为’word_series’。我们想要找出这个系列中出现最频繁的名字。

使用’word_series’系列的 value_counts() 方法来获取列表中每个唯一名字的计数。我们将其返回值存储在’word_counts’变量中。

最后,我们通过使用print()函数访问’word_counts’系列的索引的第一个元素来打印最频繁的名字。

import pandas as pd

# a  list of words
names = ['Jessica Rodriguez', 'Emily Davis', 'Michael Chen', 'Samantha Lee', 'Michael Chen', 'David Brown', 'William Wilson', 'Emily Davis', 'Sarah Kim', 'Jessica Rodriguez', 'Michael Chen', 'Samantha Lee', 'Sarah Kim', 'John Smith', 'Jessica Rodriguez', 'Jessica Rodriguez']

# create a Series from the list of words
word_series = pd.Series(names)

# use value_counts() to get the counts of each unique word
word_counts = word_series.value_counts()

# print the counts
print("Most frequent name is", word_counts.index[0])

输出

Most frequent name is Jessica Rodriguez

方法2

显示pandas系列中最频繁的值的另一种方法是使用mode()方法。value_counts()方法和mode()方法之间的区别在于,mode()方法仅返回最频繁的值,如果存在并列,则返回多个值,而不是每个唯一值的计数。

语法

要使用mode()方法显示最频繁的值,需要遵循以下语法:

mode = s.mode()[0]
print(mode)

我们在一个 series ‘s’ 上使用 ‘mode()’ 方法来找出最频繁出现的值。它的返回值的第一个元素就是 ‘s’ 的众数。然后我们使用 print() 函数来打印出这个值。

示例

在这个示例中,我们使用 Pandas 库的 Series() 函数来创建一个 Pandas series。我们将一个包含一些重复的随机整数的列表传递给 Series() 函数,它将把它创建成一个 series 数据结构,并保存在变量 ‘s’ 中。然后我们将使用 ‘s.mode()[0]’ 来获取 series 中最频繁出现的值。

最后,我们将使用 print() 函数来显示众数或最频繁出现的值。

import pandas as pd

# create a Series with some repeated values
s = pd.Series([1, 2, 2, 3, 3, 3, 4])

# use value_counts() to get the counts of each unique value
mode = s.mode()[0]

# print the most frequent value
print("The mode of the given series is", mode)

输出

The mode of the given series is 3

示例

在此示例中,我们使用了一些重复的人的出生年份的样本数据。我们将这些数据作为列表传递给Pandas Series()函数,并将返回的系列存储在变量’s’中。然后,我们将使用’mode()’方法在’s’上获取出现最频繁的出生年份,并将其存储在变量’mode’中。

最后,print()函数显示了样本数据中最常见的值。

import pandas as pd

# sample data of birth years
year_of_birth = [1990, 1992, 1993, 1993, 1994, 1995, 1995, 1995, 1996, 1997, 1997, 1998, 1999, 2000, 2000, 2001, 2002, 2002]

# create a Series with some repeated values
s = pd.Series(year_of_birth)

# use value_counts() to get the counts of each unique value
mode = s.mode()[0]

# print the most frequent value
print("The most common birth year is", mode)

输出

The most common birth year is 1995

结论

我们学习了如何使用不同的方法来显示Pandas系列数据结构中最常见的值。我们还学习了如何使用Pandas Series()函数来创建具有自定义数据的系列。上面讨论的方法在我们需要找到数据集中最常出现的元素时非常有用,这对于数据分析师或从事数据工作的人们非常有帮助。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程