Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Check If Pandas Index Only Consists of Numeric Data



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
Updated on: 2021-10-13T07:32:57+05:30

566 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements