Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - Calculate the maximum of column values of a Pandas DataFrame
To find the maximum value in Pandas DataFrame columns, use the max() function. This method works on individual columns or across the entire DataFrame to identify the highest values.
Basic DataFrame Creation
First, let's create a sample DataFrame with car data ?
import pandas as pd
# Create DataFrame with car data
car_data = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
"Units": [100, 150, 110, 80, 110, 90]
})
print("Car DataFrame:")
print(car_data)
Car DataFrame:
Car Units
0 BMW 100
1 Lexus 150
2 Audi 110
3 Tesla 80
4 Bentley 110
5 Jaguar 90
Finding Maximum of a Single Column
Use max() to find the highest value in a specific column ?
import pandas as pd
car_data = pd.DataFrame({
"Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],
"Units": [100, 150, 110, 80, 110, 90]
})
# Find maximum units sold
max_units = car_data['Units'].max()
print(f"Maximum Units sold: {max_units}")
Maximum Units sold: 150
Finding Maximum Across Multiple Columns
You can find the maximum values for all numeric columns at once ?
import pandas as pd
# Create DataFrame with multiple numeric columns
product_data = pd.DataFrame({
"Product": ['TV', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD', 'SSD'],
"Price": [8000, 500, 3000, 1500, 3000, 4000],
"Stock": [50, 200, 100, 150, 80, 120]
})
print("Product DataFrame:")
print(product_data)
print("\nMaximum values for all numeric columns:")
print(product_data.max(numeric_only=True))
Product DataFrame: Product Price Stock 0 TV 8000 50 1 PenDrive 500 200 2 HeadPhone 3000 100 3 EarPhone 1500 150 4 HDD 3000 80 5 SSD 4000 120 Maximum values for all numeric columns: Price 8000 Stock 200 dtype: int64
Finding Maximum with Row Information
To get the entire row containing the maximum value, use idxmax() ?
import pandas as pd
product_data = pd.DataFrame({
"Product": ['TV', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD', 'SSD'],
"Price": [8000, 500, 3000, 1500, 3000, 4000],
"Stock": [50, 200, 100, 150, 80, 120]
})
# Find index of row with maximum price
max_price_index = product_data['Price'].idxmax()
print(f"Index of maximum price: {max_price_index}")
# Get the complete row with maximum price
max_price_row = product_data.loc[max_price_index]
print(f"\nProduct with highest price:")
print(max_price_row)
Index of maximum price: 0 Product with highest price: Product TV Price 8000 Stock 50 Name: 0, dtype: object
Comparison of Methods
| Method | Returns | Use Case |
|---|---|---|
max() |
Maximum value | Get the highest numeric value |
idxmax() |
Index of maximum | Find position of maximum value |
loc[idxmax()] |
Complete row | Get entire record with maximum value |
Conclusion
Use max() to find maximum column values in Pandas DataFrames. Combine with idxmax() to retrieve complete rows containing maximum values for detailed analysis.
Advertisements
