Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Python - Cách truy cập phần tử cuối trong Pandas Series

Ta dùng thuộc tính iat để truy cập phần tử cuối, vì nó truy xuất giá trị đơn cho cặp hàng/cột theo vị trí nguyên.

Nhập thư viện Pandas:

import pandas as pd

Tạo Series:

data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100])

Lấy phần tử cuối bằng iat():

data.iat[-1]

Ví dụ

import pandas as pd

data = pd.Series([10, 20, 5, 65, 75, 85, 30, 100])
print("Series...\n", data)

print("The first element in the series =", data.iat[0])
print("The last element in the series =", data.iat[-1])

Đầu ra

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