
- 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
Python Pandas - Check if the index has NaNs
To check if the index has NaNs, use the index.hasnans property in Pandas.
At first, import the required libraries −
import pandas as pd import numpy as np
Creating the index. For NaN, we have used numpy library −
index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship'])
Display the index −
print("Pandas Index...\n",index)
Check if the index is having NaNs −
print("\nIs the Pandas index having NaNs?\n",index.hasnans)
Example
Following is the code −
import pandas as pd import numpy as np # Creating the index # For NaN, we have used numpy library index = pd.Index(['Car','Bike', np.nan,'Car',np.nan, 'Ship']) # Display the index print("Pandas Index...\n",index) # Return an array representing the data in the Index print("\nArray...\n",index.values) # Check if the index is having NaNs print("\nIs the Pandas index having NaNs?\n",index.hasnans) # Return a tuple of the shape of the underlying data print("\nA tuple of the shape of underlying data...\n",index.shape)
Output
This will produce the following code −
Pandas Index... Index(['Car', 'Bike', nan, 'Car', nan, 'Ship'], dtype='object') Array... ['Car' 'Bike' nan 'Car' nan 'Ship'] Is the Pandas index having NaNs? True A tuple of the shape of underlying data... (6,)
- Related Articles
- Python - Check if the Pandas Index with some NaNs is a floating type
- Python Pandas - Check if the index has unique values
- Python Pandas - Check if the index has duplicate values
- Python Pandas - Check if the Pandas Index holds Interval objects
- Python - Check if the Pandas Index holds categorical data
- Python Pandas - Check if the IntervalIndex has overlapping intervals
- Python - Check if the Pandas Index only consists of booleans
- Python - Check if the Pandas Index is a floating type
- Python - Check if the Pandas Index only consists of integers
- Python - Check if the Pandas Index is of the object dtype
- Python - Check if the Pandas Index only consists of numeric data
- Python Pandas - Check if the index is empty with 0 elements
- Python Pandas – Check and Display row index with infinity
- Python Pandas - Determine if two Index objects are equal
- Python - Check if Pandas dataframe contains infinity

Advertisements