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
Write a program in Python to find the lowest value in a given DataFrame and store the lowest value in a new row and column
In this tutorial, we'll learn how to find the minimum values in a Pandas DataFrame and store them in both a new column and a new row. This is useful for data analysis when you need to track minimum values across rows and columns.
Creating the Sample DataFrame
Let's start by creating a sample DataFrame to work with ?
import pandas as pd
data = [[12, 13, 5], [10, 6, 4], [16, 18, 20], [11, 15, 58]]
df = pd.DataFrame(data, columns=['one', 'two', 'three'])
print("Original DataFrame:")
print(df)
Original DataFrame: one two three 0 12 13 5 1 10 6 4 2 16 18 20 3 11 15 58
Adding a Column with Row-wise Minimum Values
To find the minimum value in each row and store it as a new column, we use min(axis=1) where axis=1 operates across columns ?
import pandas as pd
data = [[12, 13, 5], [10, 6, 4], [16, 18, 20], [11, 15, 58]]
df = pd.DataFrame(data, columns=['one', 'two', 'three'])
# Add new column to store minimum value of each row
df['min_value'] = df.min(axis=1)
print("DataFrame with minimum column added:")
print(df)
DataFrame with minimum column added: one two three min_value 0 12 13 5 5 1 10 6 4 4 2 16 18 20 16 3 11 15 58 11
Adding a Row with Column-wise Minimum Values
To add a new row containing the minimum value of each column, we use min(axis=0) where axis=0 operates across rows ?
import pandas as pd
data = [[12, 13, 5], [10, 6, 4], [16, 18, 20], [11, 15, 58]]
df = pd.DataFrame(data, columns=['one', 'two', 'three'])
# Add column with row-wise minimums
df['min_value'] = df.min(axis=1)
# Add new row with column-wise minimums
df.loc[len(df)] = df.min(axis=0)
print("Final DataFrame with both minimum column and row:")
print(df)
Final DataFrame with both minimum column and row: one two three min_value 0 12 13 5 5 1 10 6 4 4 2 16 18 20 16 3 11 15 58 11 4 10 6 4 4
How It Works
The solution uses two key Pandas operations:
df.min(axis=1)− Finds minimum values across columns (row-wise)df.min(axis=0)− Finds minimum values across rows (column-wise)df.loc[len(df)]− Adds a new row at the end of the DataFrame
Complete Example
import pandas as pd
# Create sample DataFrame
data = [[12, 13, 5], [10, 6, 4], [16, 18, 20], [11, 15, 58]]
df = pd.DataFrame(data, columns=['one', 'two', 'three'])
print("Step 1: Original DataFrame")
print(df)
print()
print("Step 2: Add column with row-wise minimums")
df['min_value'] = df.min(axis=1)
print(df)
print()
print("Step 3: Add row with column-wise minimums")
df.loc[len(df)] = df.min(axis=0)
print(df)
Step 1: Original DataFrame one two three 0 12 13 5 1 10 6 4 2 16 18 20 3 11 15 58 Step 2: Add column with row-wise minimums one two three min_value 0 12 13 5 5 1 10 6 4 4 2 16 18 20 16 3 11 15 58 11 Step 3: Add row with column-wise minimums one two three min_value 0 12 13 5 5 1 10 6 4 4 2 16 18 20 16 3 11 15 58 11 4 10 6 4 4
Conclusion
Use df.min(axis=1) to find row-wise minimums and df.min(axis=0) for column-wise minimums. The loc method allows you to add new rows with calculated values to your DataFrame.
