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 –
- Using
is_numeric_dtype()
method from the pandas module - Using
np.issubdtype()
method from the numpy module - 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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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:
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:
- Using
is_numeric_dtype()
method from the pandas module. - Using
np.issubdtype()
method from the numpy module. - Using
.dtype.kind
attribute of the pandas dataframe object.
You might also be interested in –
- Pandas – Select Columns of a Specific Type
- Pandas – Change Column Type to Category
- Pandas – Get Columns with Missing Values
- Pandas – Get dataframe summary with info()
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.