Python - 如何訪問 Pandas 數列中的最後一個元素?
我們將使用 iat 屬性來訪問最後一個元素,因為它用於透過整數位置訪問行/列對的單個值。
讓我們首先匯入所需的 Pandas 庫 −
import pandas as pd
使用數字建立一個 Pandas 數列 −
data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100])
現在,使用 iat() 獲取最後一個元素 −
data.iat[-1]
示例
以下為其程式碼 −
import pandas as pd # pandas series data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100]) print"Series...\n",data # get the first element print"The first element in the series = ", data.iat[0] # get the last element print"The last element in the series = ", data.iat[-1]
輸出
此程式碼將產生以下輸出 −
Series... 0 10 1 20 2 5 3 65 4 75 5 85 6 30 7 100 dtype: int64 The first element in the series = 10 The last element in the series = 100
廣告