如何在 Pandas 系列中顯示最常出現的值?
在本教程中,我們將學習如何在 Python 的幫助下顯示 Pandas 系列中最常出現的值。在本教程中,我們將使用 Pandas 庫。
系列是 Pandas 中的一種資料結構,類似於 Excel 表格或 SQL 表格中的一列。它是一種一維帶標籤的資料結構,可以容納不同的資料型別,例如整數、浮點數、字串等。
最常出現的值是在系列中出現次數最多的值。用數學術語來說,它是資料的眾數。
方法 1
在 Pandas 系列中顯示最常出現值的一種方法是使用 value_counts() 方法。它返回一個系列,其中包含每個唯一值的計數,並按降序排序。它包含系列中的原始值作為其索引。
語法
要使用 value_counts() 方法顯示影像,您需要遵循以下語法:
counts = s.value_counts() print(counts.index[0])
我們在系列 '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 系列資料結構。此係列稱為 '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("The mode of the given series is", mode)
輸出
The mode of the given series is 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("The most common birth year is", mode)
輸出
The most common birth year is 1995
結論
我們學習瞭如何使用不同的方法來顯示 Pandas 系列資料結構中最常出現的值。我們還學習瞭如何使用 Pandas Series() 函式使用自定義資料建立系列。當我們必須在資料集中找到最常出現的元素時,上面討論的方法非常方便,這對於資料分析師或處理資料的人員非常有用。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP