如何在Pandas系列中显示最常见的值?
本教程将介绍如何在Python的帮助下显示Pandas序列中最常见的值,我们将使用Pandas库。
序列是一种数据结构,类似于Excel表或SQL表中的列。 它是一个一维标记的数据结构,可以保存不同的数据类型,例如整数,浮点数,字符串等。
最常见的值是系列中出现最频繁的值。 在数学术语中,它是数据的众数。
方法1
在Pandas系列中显示最常见的值的一种方法是使用value_counts()方法。 它返回一个包含每个唯一值计数的系列,按降序排序。 它包含系列中的原始值作为其索引。
语法
要使用value_counts()方法显示图像,需要遵循以下语法 –
counts = s.value_counts()
print(counts.index[0])
我们使用value_counts()方法在系列”s”上查找最常见的值。”counts.index[0]”将返回counts中第一个值的索引。然后我们使用print()函数将其打印出来。
示例
在此示例中,我们使用Pandas library的Series()函数创建Pandas series,将一系列随机整数传递给Series()函数,返回一个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系列数据结构。此系列称为“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)
我们在想要找到最频繁的值的系列’s’上使用’mode()’方法。它的返回值中的零号元素将是’s’的众数。然后,我们将使用print()函数打印它。
例子
在这个例子中,我们使用Pandas库的Series()函数创建Pandas系列。我们传递了一个包含某些重复的随机整数列表给Series()函数,它将其创建为系列数据结构,我们把它存储在’s’变量中。然后,我们将使用’s.mode()[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
mode = s.mode()[0]
# print the most frequent value
print("给定系列的众数是", mode)
输出
给定系列的众数是3
例子
在这个例子中,我们使用具有一些重复的出生年份示例数据。我们将把这些数据作为列表传递给Pandas Series()函数,并将返回的系列存储在变量’s’中。然后,我们将在’s’上使用mode()方法来获取最常见的出生年份,并将其存储在’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("最常见的出生年份是", mode)
输出
最常见的出生年份是1995
结论
我们学习了如何使用不同的方法显示Pandas系列数据结构中最频繁的值。我们还学习了如何使用Pandas Series()函数创建具有自定义数据的系列。以上讨论的方法在我们需要找到数据集中最常出现的元素时非常有用,这对于数据分析师或与数据相关的人员非常有帮助。