Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python - จะเข้าถึงองค์ประกอบสุดท้ายในซีรีย์ Pandas ได้อย่างไร


เราจะใช้ iat แอตทริบิวต์เพื่อเข้าถึงองค์ประกอบสุดท้าย เนื่องจากใช้เพื่อเข้าถึงค่าเดียวสำหรับคู่แถว/คอลัมน์ตามตำแหน่งจำนวนเต็ม

ให้เรานำเข้าไลบรารี Pandas ที่จำเป็นก่อน -

import pandas as pd

สร้างชุดหมีแพนด้าด้วยตัวเลข -

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