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

Python Pandas - หาตำแหน่งค่าสูงสุดในดัชนี (index.argmax)

ในการหาตำแหน่ง (integer position) ของค่าสูงสุดในดัชนี (Index) ให้ใช้เมธอด index.argmax()

ขั้นตอนการทำงาน

1. นำเข้าไลบรารีที่จำเป็น

import pandas as pd

2. สร้างดัชนี

index = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55])

3. แสดงดัชนี

print("Pandas Index...\n", index)

4. หาตำแหน่งค่าสูงสุด

print("\nGet the int position of the largest value in the Index...\n", index.argmax())

ตัวอย่างโค้ดสมบูรณ์

import pandas as pd

# สร้างดัชนี
index = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55])

# แสดงดัชนี
print("Pandas Index...\n", index)

# แสดงรูปร่างของข้อมูล
print("\nA tuple of the shape of underlying data...\n", index.shape)

# แสดงจำนวนองค์ประกอบ
print("\nNumber of elements in the index...\n", index.size)

# หาตำแหน่งค่าสูงสุด
print("\nGet the int position of the largest value in the Index...\n", index.argmax())

ผลลัพธ์

Pandas Index... Int64Index([15, 25, 55, 10, 100, 70, 35, 40, 55], dtype='int64')
A tuple of the shape of underlying data... (9,)
Number of elements in the index... 9
Get the int position of the largest value in the Index... 4

คำอธิบาย

ค่าสูงสุดคือ 100 อยู่ที่ตำแหน่งที่ 4 (นับจาก 0) ดังนั้น argmax() คืนค่า 4