How to select rows with one or more nulls from a Pandas DataFrame without listing columns explicitly?

Given a DataFrame with some null values in some rows, we need to select those null values.
Submitted by Pranit Sharma, on May 15, 2022

Rows in pandas are the different cell (column) values that are aligned horizontally and also provide uniformity. Each row can have the same or different value. Rows are generally marked with the index number but in pandas we can also assign index names according to the needs. In pandas, we can create, read, update and delete a column or row value.

For this purpose, we will use pandas.isnull() method. This method is used to traverse along the entire DataFrame and returns another DataFrame with Boolean values. It returns True if there is some NaN value and False if not.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example:

# Importing pandas package
import pandas as pd

# To create NaN values, you must import numpy package, 
# then you will use numpy.NaN to create NaN values
import numpy as np

# Create an empty list
result = []

# Creating a dictionary with some NaN values
d={
    "Name":['Hari','Mohan','Neeti','Shaily'],
    "Age":[25,np.NaN,np.NaN,21],
    "Gender":['Male','Male',np.NaN,'Female'],
    "Profession":['Doctor','Teacher','Singer',np.NaN]
}

# Now we will create DataFrame
df = pd.DataFrame(d)

# Viewing the original DataFrame
print("Original DataFrame:\n")
print(df,"\n\n")

# Using pd.isnull(df)
print(pd.isnull(df))

Output:

Example: Select rows with one or more nulls from a Pandas DataFrame

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.