

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas – Check for Null values using notnull()
The notnull() method returns a Boolean value i.e. if the DataFrame is having null value(s), then False is returned, else True.
Let’s say the following is our CSV file with some NaN i.e. null values −
Let us first read the CSV file −
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv")
Checking for not null values −
res = dataFrame.notnull()
Now, on displaying the DataFrame, the CSV data will be displayed in the form of True and False i.e. boolean values because notnull() returns boolean. For Null values, False will get displayed. For Not-Null values, True will get displayed.
Example
Following is the complete code −
import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv") print("DataFrame...\n",dataFrame) # count the rows and columns in a DataFrame print("\nNumber of rows and column in our DataFrame = ",dataFrame.shape) res = dataFrame.notnull() print("\nDataFrame displaying False for Null (NaN) value = \n",res) dataFrame = dataFrame.dropna() print("\nDataFrame after removing null values...\n",dataFrame) print("\n(Updated) Number of rows and column in our DataFrame = ",dataFrame.shape)
Output
This will produce the following output −
DataFrame... Car Place UnitsSold 0 Audi Bangalore 80.0 1 Porsche Mumbai 110.0 2 RollsRoyce Pune NaN 3 BMW Delhi 200.0 4 Mercedes Hyderabad 80.0 5 Lamborghini Chandigarh NaN 6 Audi Mumbai NaN 7 Mercedes Pune 120.0 8 Lamborghini Delhi 100.0 Number of rows and column in our DataFrame = (9, 3) DataFrame displaying False for Null values = Car Place UnitsSold 0 True True True 1 True True True 2 True True False 3 True True True 4 True True True 5 True True False 6 True True False 7 True True True 8 True True True DataFrame after removing null values... Car Place UnitsSold 0 Audi Bangalore 80.0 1 Porsche Mumbai 110.0 3 BMW Delhi 200.0 4 Mercedes Hyderabad 80.0 7 Mercedes Pune 120.0 8 Lamborghini Delhi 100.0 (Updated)Number of rows and column in our DataFrame = (6, 3)
- Related Questions & Answers
- Python Pandas – Propagate non-null values backward
- Python Pandas – Propagate non-null values forward
- Check for NULL or NOT NULL values in a column in MySQL
- How do I check for null values in JavaScript?
- Python - Remove a column with all null values in Pandas
- Check for null in MongoDB?
- Python Pandas - Check if the index has duplicate values
- Python Pandas - Check if the index has unique values
- Display 1 for NULL values in MySQL
- How does the pandas series.equals() method handle the null values?
- MySQL query to set values for NULL occurrence
- MySQL update column to NULL for blank values
- Python - Display True for infinite values in a Pandas DataFrame
- Using NULL Values in Perl Database Operation
- Performing Null check using HANA SQL Script
Advertisements