get data type of a column in a pandas dataframe

Pandas – Get Column data type

In this tutorial, we will know how to get the data type of a column in a Pandas Dataframe.
Later, we will understand the same with the help of a few examples.

How to get column dtype in a Pandas Dataframe?

You can get the dtype of a pandas datafrmae column using the column dtype attribute and the dtypes of all the columns with the dataframe .dtypes attribute.

1. .dtype attribute :

You can get the column data type of a particular column, using the .dtype attribute along with the dataframe name and column name specified.

Syntax: dataFrameName['ColumnName'].dtype

2. .dtypes attribute:

In case you want to know the data types of all columns in the dataframe, you can use the .dtypes attribute along with the dataframe name.

Syntax: dataFrameName.dtypes

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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

We will now look at a few examples for a better understanding.

But before that, we will create a pandas dataframe that we will be using throughout this tutorial using the following command:

import pandas as pd

# employee data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32],
    "Department": ["Sales", "Sales", "Accounting", "HR"]
}

# create pandas dataframe
df = pd.DataFrame(data)
# displays dataframe
df

Output:

dataframe with employee data

Example 1: Get the data type of a specific column in dataframe

get data type of a column in a pandas dataframe

Let’s get the dtype of the “Age” column from the above dataframe.

# dtype of the "Age" column
df['Age'].dtype

Output:

dtype('int64')

Example 2: Get the data type of all columns in the dataframe

Let’s now get the dtypes of all the columns present in the dataframe above.

# dtypes of all the columns in the dataframe
df.dtypes

Output:

Name          object
Age            int64
Department    object
dtype: object

Summary

In this tutorial, we looked at how to get column data type(s) in a Pandas dataframe. Following are the key takeaways –

  • Use the .dtype attribute to get the data type of a particular column in a Pandas dataframe. First, select the column and then use this attribute to get its dtype.
  • Use the dataframe .dtypes attribute to get the data type of all columns in a Pandas dataframe.

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

Scroll to Top