
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to find which column has the minimum number of missing values in a given dataframe
Assume, you have a dataframe and the minimum number of missing value column is,
DataFrame is: Id Salary Age 0 1.0 20000.0 22.0 1 2.0 NaN 23.0 2 3.0 50000.0 NaN 3 NaN 40000.0 25.0 4 5.0 80000.0 NaN 5 6.0 NaN 25.0 6 7.0 350000.0 26.0 7 8.0 55000.0 27.0 8 9.0 60000.0 NaN 9 10.0 70000.0 24.0 lowest missing value column is: Id
To solve this, we will follow the steps given below −
Solution
Define a dataframe with three columns Id,Salary and Age
Set df.apply() inside lambda function to check the sum of null values from all rows
df = df.apply(lambda x: x.isnull().sum(),axis=0)
Finally, print the lowest value from the df using df.idxmin()
df.idxmin()
Example
Let’s see the below code to get a better understanding −
import pandas as pd import numpy as np df = pd.DataFrame({'Id':[1,2,3,np.nan,5,6,7,8,9,10], 'Salary':[20000,np.nan,50000,40000,80000,np.nan,350000,55000,60000,70000], 'Age': [22,23,np.nan,25,np.nan,25,26,27,np.nan,24] }) print("DataFrame is:\n",df) df = df.apply(lambda x: x.isnull().sum(),axis=0) print("lowest missing value column is:",df.idxmin())
Output
DataFrame is: Id Salary Age 0 1.0 20000.0 22.0 1 2.0 NaN 23.0 2 3.0 50000.0 NaN 3 NaN 40000.0 25.0 4 5.0 80000.0 NaN 5 6.0 NaN 25.0 6 7.0 350000.0 26.0 7 8.0 55000.0 27.0 8 9.0 60000.0 NaN 9 10.0 70000.0 24.0 lowest missing value column is: Id
- Related Articles
- Write a Python code to fill all the missing values in a given dataframe
- Write a program in Python to find the minimum rank of a particular column in a dataframe
- Write a program in Python to find the minimum age of an employee id and salary in a given DataFrame
- Python - Calculate the minimum of column values of a Pandas DataFrame
- Write a program in Python to count the total number of leap years in a given DataFrame
- Write a Python program to sort a given DataFrame by name column in descending order
- Write a Python code to find the second lowest value in each column in a given dataframe
- Write a program in C++ to find the missing positive number in a given array of unsorted integers
- Write a program in Java to find the missing positive number in a given array of unsorted integers
- Write a program in Python to covert the datatype of a particular column in a dataframe
- Write a program in Python to filter City column elements by removing the unique prefix in a given dataframe
- Write a Python program to trim the minimum and maximum threshold value in a dataframe
- Write a program in Python to split the date column into day, month, year in multiple columns of a given dataframe
- Write a Python program to reshape a given dataframe in different ways
- 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

Advertisements