การหาค่าสูงสุดใน Pandas Index
ใช้เมธอด index.max() เพื่อคืนค่าสูงสุดของ Index ที่เป็นตัวเลข
ขั้นตอน
- นำเข้า pandas
- สร้าง Index แบบตัวเลข
- แสดง Index
- ใช้
max()หาค่าสูงสุด
ตัวอย่างโค้ด
import pandas as pd
# สร้าง Index แบบจำนวนเต็ม
index = pd.Index([10, 20, 70, 40, 90, 50, 25, 30])
# แสดง Index
print("Pandas Index...\n", index)
# แสดงจำนวนองค์ประกอบและ dtype
print("\nNumber of elements in the index...\n", index.size)
print("\nThe dtype object...\n", index.dtype)
# หาค่าสูงสุด
print("\nMaximum value..\n", index.max())
ผลลัพธ์
Pandas Index... Int64Index([10, 20, 70, 40, 90, 50, 25, 30], dtype='int64') Number of elements in the index... 8 The dtype object... int64 Maximum value.. 90
หมายเหตุ
เมธอด max() ทำงานกับ Index ที่มีค่าตัวเลขเท่านั้น หาก Index เป็นสตริงจะเปรียบเทียบตามลำดับอักขระ