Drop row if two columns are NaN

Given a pandas dataframe, we have to drop row if two columns are NaN.
Submitted by Pranit Sharma, on October 12, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

While creating a DataFrame or importing a CSV file, there could be some NaN values in the cells. NaN values mean “Not a Number” which generally means that there are some missing values in the cell.

Problem statement

Suppose, we are given a DataFrame with multiple columns and we need to drop those rows for which multiple columns have NaN values.

Dropping row if two columns are NaN

To drop row if two columns are NaN, we will first create a DataFrame and then we will use the dropna() method inside which we will pass a subset as those columns which we want to check.

The dropna() method is used to remove nan values from a DataFrame.

Let us understand with the help of an example,

Python program to drop row if two columns are NaN

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating two dictionary
d = {
    'a':[0.9,0.8,np.nan,1.1,0],
    'b':[0.3,0.5,np.nan,1,1.2],
    'c':[0,0,1.1,1.9,0.1],
    'd':[9,8,0,0,0]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display DataFrame
print("DataFrame1:\n",df,"\n")

# dropping rows for column a and b
res = df.dropna(subset=['a', 'b'], how='all')

# display result
print("Result:\n",res)

Output

Example: Drop row if two columns are NaN

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.