Pandas 系列中 tail() 方法有什麼作用?
Pandas 系列物件中的 tail 方法用於從系列中檢索底部元素。並且此 tail 方法將一個整數作為引數,該引數由變數 n 表示。
根據該 n 值,pandas 系列 tail 方法將返回一個系列物件,其中包含實際系列物件中的 n 個底部元素。
讓我們舉一個例子,看看這個 tail 方法如何在我們的 pandas 系列物件上工作。
示例
# importing required packages
import pandas as pd
# creating pandas Series object
series = pd.Series(list(range(10,100,4)))
print(series)
print('
Result from tail() method:')
# accessing bottom elements by using til method
print(series.tail())解釋
這裡我們使用 python 列表和 range 函式建立了一個 pandas 系列物件,並將 tail 方法定義為我們的系列物件,如下所示“series.tail()”。我們空指定 tail 方法,以便我們可以從我們的系列物件中獲取 5 個底部元素。讓我們檢查輸出塊。
輸出
0 10 1 14 2 18 3 22 4 26 5 30 6 34 7 38 8 42 9 46 10 50 11 54 12 58 13 62 14 66 15 70 16 74 17 78 18 82 19 86 20 90 21 94 22 98 dtype: int64 Result from tail() method: 18 82 19 86 20 90 21 94 22 98 dtype: int64
上面的輸出塊中有兩個系列物件,一個是整個系列物件的輸出,第二個是 tail 方法的輸出。在第二部分,我們可以看到從索引 18 到 22 的 5 個底部元素。
示例
series.tail(2)
解釋
我們可以將一個整數值作為引數指定給 tail 方法。它用於限制輸出元素的數量。因此,根據此引數,我們可以看到系列物件的特定數量的底部元素。
輸出
21 94 22 98 dtype: int64
這些是系列物件的 2 個底部元素,它們是從 tail 方法中檢索到的。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP