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

คำนวณค่าสูงสุดของคอลัมน์ใน Pandas DataFrame ด้วย max()

ใช้เมธอด max() ของ Series (คอลัมน์ DataFrame) เพื่อหาค่าสูงสุดของข้อมูลตัวเลขในคอลัมน์นั้นๆ

ตัวอย่างโค้ด

import pandas as pd

# DataFrame 1
df1 = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
    "Units": [100, 150, 110, 80, 110, 90]
})
print("DataFrame 1:\n", df1)
print(f"\nค่าสูงสุดของ Units: {df1['Units'].max()}")

# DataFrame 2
df2 = pd.DataFrame({
    "Product": ['TV', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD', 'SSD'],
    "Price": [8000, 500, 3000, 1500, 3000, 4000]
})
print("\nDataFrame 2:\n", df2)
print(f"\nค่าสูงสุดของ Price: {df2['Price'].max()}")

ผลลัพธ์

DataFrame 1:
       Car  Units
0      BMW    100
1    Lexus    150
2     Audi    110
3    Tesla     80
4  Bentley    110
5   Jaguar     90

ค่าสูงสุดของ Units: 150

DataFrame 2:
    Product  Price
0        TV   8000
1  PenDrive    500
2  HeadPhone  3000
3  EarPhone  1500
4       HDD  3000
5       SSD  4000

ค่าสูงสุดของ Price: 8000

ค่าสูงสุดของ Units คือ 150 (Lexus) และ Price คือ 8000 (TV)