Skip to Content

Pandas – Check if column datatype is numeric

In this tutorial, we’ll try to have a look at different methods used to check if the dtype of a column in a pandas dataframe is numeric or not.

How to check if a column datatype is numeric?

To check if a specific datatype of a column in the pandas dataframe is numeric or not we can use the following methods –

  1. Using is_numeric_dtype() method from the pandas module
  2. Using np.issubdtype() method from the numpy module
  3. Using .dtype.kind attribute of the pandas dataframe object

Method – 1: Using pandas is_numeric_dtype()

Sample Code:

  import pandas as pd
  from pandas.api.types import is_numeric_dtype

  is_numeric_dtype(df['Col'])

In this method, we use the is_numeric_dtype() function available in the pandas.api.types module which returns a boolean value of True if the datatype of elements in the column is numeric and False otherwise. Pass the column as an argument.

Method – 2: Using np.issubdtype()

Sample Code:

np.issubdtype(df['Col'].dtype, np.number)  # where df['X'] is a pandas Series

In this method, use the issubdtype() function available in the numpy module and check if the data type of elements in the column is np.number as the issubdtype() function returns True if the datatype of elements in the column is Numeric and False otherwise.

Method – 3: Using .dtype.kind

Sample Code:

df['Col'].dtype.kind in 'biufc'

In this method, we use the .dtype.kind attribute to check if the column datatype is in biufc where biufc : b bool, i int (signed), u unsigned int, f float, c complex.

Examples

Now, Let us look at examples of Each method discussed above to check if specific column elements are of Numeric datatype or not.

Note: In case, you don’t have a pandas Dataframe, use the below simple method to create one. we use this dataframe for all the examples mentioned below

import pandas as pd
df = pd.DataFrame({'Col1': ['a', 'b', 'c'], 'Col2': [1.0, 2.0, 3.0]})
df

Output:

pandas dataframe with a string and a numeric column

Using is_numeric_dtype()

Example – 1

In this method, we can simply import the is_numeric_dtype function from pandas.api.types module and directly pass the specific column to the function as a parameter that returns the boolean value of True or False.

Code:

is_numeric_dtype(df['Col2'])

Output:

True

Example – 2

Code:

is_numeric_dtype(df['Col1'])

Output:

False

Using np.issubdtype()

Example – 1

In this method, we can simply import the numpy module and directly use the np.issubdtype function by passing the specific column to the function as a parameter along with the np.number which returns the boolean value of True or False.

Code:

import numpy as np
np.issubdtype(df['Col2'].dtype, np.number)

Output:

True

Example – 2

Code:

import numpy as np
np.issubdtype(df['Col1'].dtype, np.number)

Output:

False

Using .dtype.kind

Example – 1

In this method, we don’t need to import any functions instead use the built-in property of the pandas dataframe object called .dtype.kind which directly returns the boolean value of True or False.

Code:

df['Col2'].dtype.kind in 'biufc'

Output:

True

Example – 2

Code:

df['Col1'].dtype.kind in 'biufc'

Output:

False

Summary

In this tutorial, we looked at 3 different ways to check if the elements in a specific column of a pandas dataframe are of numeric dtype or not.
They are:

  1. Using is_numeric_dtype() method from the pandas module.
  2. Using np.issubdtype() method from the numpy module.
  3. Using .dtype.kind attribute of the pandas dataframe object.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Chaitanya Betha

    I'm an undergrad student at IIT Madras interested in exploring new technologies. I have worked on various projects related to Data science, Machine learning & Neural Networks, including image classification using Convolutional Neural Networks, Stock prediction using Recurrent Neural Networks, and many more machine learning model training. I write blog articles in which I would try to provide a complete guide on a particular topic and try to cover as many different examples as possible with all the edge cases to understand the topic better and have a complete glance over the topic.