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

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

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

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

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 smallest value in the Index...\n", index.argmin())

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

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 smallest value in the Index...\n", index.argmin())

ผลลัพธ์

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 smallest value in the Index... 3

คำอธิบาย

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