In this tutorial, you will learn to check if a column has object dtype. It is recommended that you have good exposure to Python, but this is not necessary.
How to check if a Pandas DataFrame column has an object dtype?
To check if a Pandas DataFrame column has an object dtype, we will use Pandas built-in is_object_dtype()
function. The following is the syntax-
is_col_object_dtype = is_object_dtype(df[column])
Here,
is_object_dtype()
– function to check if a Pandas Series has object dtype.df
– a Pandas DataFrame.df[column]
– dataframe’s column.df[column]
is a Pandas Series representing the column. When we pass it to the functionis_object_dtype()
, the function returnsTrue
if this column has a dtype object; else it returnsFalse
.
Another way to do this is by using the equality operator, ==
. The syntax is –
is_col_object_dtype = (df[column].dtype == object)
Here,
df
– a Pandas DataFrame.df[column]
– Returns dataFrame column as Pandas Series.df[column].dtype
– Returns the dtype of the column.object
– An inbuilt base class in Python from which all the base classes are built.
Here, df[column].dtype
returns a numpy.dtype object. This contains the dtype of the column. NumPy allows you to compare the dtypes of objects with the type identifiers of Python. We used this ability to compare the dtype of the column with the object
identifier.
Examples
Let’s use some examples to understand the above syntax. We will create a dataframe for a restaurant’s sales as an example. To keep it simple, we only used five entries.
import pandas as pd from pandas.api.types import is_object_dtype #Create a dictionary to make a dataframe d = { "Item" : ["Pulao", "Masala Dosa", "Idli", "Onion Dosa", "Thali"], "Cost": [419, 369, 199, 359, 689], "Service Charges" : [20.05, 18.45, 9.95, 17.55, 34.50], } #Create the dataframe from dictionary 'd' df = pd.DataFrame(d) #Print the dataframe print(df)
Output:
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.
Item Cost Service Charges 0 Pulao 419 20.05 1 Masala Dosa 369 18.45 2 Idli 199 9.95 3 Onion Dosa 359 17.55 4 Thali 689 34.50
In the above output, we can see that the dataframe has been created. As you will see later, the ‘Cost’ and the ‘Service Charges’ columns have int and float dtypes respectively, whereas the ‘Item’ column has an object dtype. By default, all the Pandas columns having at least one non-numeric or non-datetime type cell have a dtype as object.
Example 1: Check if the dataframe column has object dtype using Pandas inbuilt function
To check if the column has a datetime dtype, pass the column to the Pandas function is_object_dtype()
.
is_col_object_dtype = is_object_dtype(df["Item"]) print(is_col_object_dtype)
Output:
True
The result is True
, indicating that the column has a dtype object. If the column doesn’t have a dtype object, then the result will be False
. Let’s pass the ‘Cost’ column, which has an int dtype, to the function.
is_col_object_dtype = is_object_dtype(df["Cost"]) print(is_col_object_dtype)
Output:
False
As expected, we get False
as result.
Example 2: Check if the dataframe column has object dtype using the equality operator
Instead of using Pandas’ built-in function, let’s use the equality operator to check the dtype of the item column is object or not.
is_col_object_dtype = (df["Item"].dtype == object) print(is_col_object_dtype)
Output:
True
The output is the same as that returned by the function is_object_dtype()
. Let’s check the ‘Service Charges’ column. Note that this column has a float dtype.
is_col_object_dtype = (df["Service Charges"].dtype == object) print(is_col_object_dtype)
Output:
False
Thus, we see that the comparison of dtypes also yields the same results as that of Pandas built in function.
Summary
From this tutorial, we looked at:
- Use Pandas’ inbuilt function
is_object_dtype()
to check if the column has an object dtype. - Use Pandas column’s dtype attribute and Python’s equality operator,
==
to check if the column has an object dtype.
You might also be interested in –
- Get Count of dtypes in a Pandas DataFrame
- Pandas – How to Create a date range?
- Pandas – Check if Column contains String from List
- Pandas – Select Columns of a Specific Type
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.