Python - Check if the Pandas Index only consists of numeric data

PythonServer Side ProgrammingProgramming

To check if the Pandas Index only consists of numeric data, use the index.is_numeric() method. At first, import the required libraries -

import pandas as pd
import numpy as np

Creating Pandas index with integer, float and NaNs

index = pd.Index([5, 10.2, 25, 50, 75.2, 100, np.nan])

Display the Pandas index −

print("Pandas Index...\n",index)

Check whether index values has only numeric data. Numeric data includes integer, floats and NaNs −

index.is_numeric()

Example

Following is the code −

import pandas as pd
import numpy as np

# Creating Pandas index with integer, float and NaNs
index = pd.Index([5, 10.2, 25, 50, 75.2, 100, np.nan])

# Display the Pandas index
print("Pandas Index...\n",index)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# Check whether index values has only numeric data.
# Numeric data includes integer, floats and NaNs
print("\nIndex values only consists of numeric data?\n",index.is_numeric())

Output

This will produce the following output −

Pandas Index...
Float64Index([5.0, 10.2, 25.0, 50.0, 75.2, 100.0, nan], dtype='float64')

Number of elements in the index...
7

The dtype object...
float64

Index values only consists of numeric data?
True
raja
Updated on 13-Oct-2021 07:32:57

Advertisements