- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Assume you have a dataframe,
one two three 0 12 13 5 1 10 6 4 2 16 18 20 3 11 15 58
The result for storing the minimum value in new row and column is −
Add new column to store min value 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 Add new row to store min value 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
Solution
To solve this, we will follow the steps given below −
Define a dataframe
Calculate the minimum value in each column and store it as new column using the following step,
df['min_value'] = df.min(axis=1)
Find the minimum value in each row and store it as new row using the below step,
df.loc[len(df)] = df.min(axis=0)
Example
Let us see the following implementation to get a better understanding,
import pandas as pd import numpy as np data = [[12,13,5],[10,6,4],[16,18,20],[11,15,58]] df = pd.DataFrame(data,columns=('one','two','three')) print("Add new column to store min value") df['min_value'] = df.min(axis=1) print(df) print("Add new row to store min value") df.loc[len(df)] = df.min(axis=0) print(df)
Output
Add new column to store min value 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 Add new row to store min value 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
Advertisements