How to Check the Data Type in Pandas DataFrame

You may use the following syntax to check the data type of all columns in pandas DataFrame:

df.dtypes

Alternatively, you may use the syntax below to check the data type of a particular column in pandas DataFrame:

df['DataFrame Column'].dtypes

Steps to Check the Data Type in Pandas DataFrame

Step 1: Gather the Data for the DataFrame

To start, gather the data for your DataFrame.

For illustration purposes, I gathered the following data about products and prices:

ProductsPrices
AAA200
BBB700
CCC400
DDD1200
EEE900

The goal is to check the data type of the above columns across multiple scenarios.

Step 2: Create the DataFrame

Next, create the actual DataFrame based on the following syntax:

import pandas as pd

Data = {'Products': ['AAA','BBB','CCC','DDD','EEE'],
          'Prices': ['200','700','400','1200','900']}

df = pd.DataFrame(Data)
print (df)

Once you run the code in Python, you’ll get this DataFrame:

DataFrame

Note that initially the values under the ‘Prices’ column were stored as strings by placing quotes around those values.

Step 3: Check the Data Type

You can now check the data type of all columns in the DataFrame by adding the following to the code:

df.dtypes

Here is the complete Python code for our example:

import pandas as pd

Data = {'Products': ['AAA','BBB','CCC','DDD','EEE'],
          'Prices': ['200','700','400','1200','900']}

df = pd.DataFrame(Data)
print (df.dtypes)

You’ll notice that the data type for both columns is ‘Object‘ which represents strings:

How to Check the Data Type in Pandas DataFrame

Let’s now remove the quotes for all the values under the ‘Prices’ column:

import pandas as pd

Data = {'Products': ['AAA','BBB','CCC','DDD','EEE'],
          'Prices': [200,700,400,1200,900]}

df = pd.DataFrame(Data)
print (df.dtypes)

After the removal of the quotes, the data type for the ‘Prices’ column would become integer:

Check the Data Type in Pandas DataFrame

Checking the Data Type of a Particular Column in Pandas DataFrame

Let’s now check the data type of a particular column (e.g., the ‘Prices’ column) in our DataFrame:

df['DataFrame Column'].dtypes

Here is the full syntax for our example:

import pandas as pd

Data = {'Products': ['AAA','BBB','CCC','DDD','EEE'],
          'Prices': [200,700,400,1200,900]}

df = pd.DataFrame(Data)
print (df['Prices'].dtypes)

The data type for the ‘Prices’ column would be integer:

Integer

But what if you want to convert the data type from integer to float?

You may then apply this template to perform the conversion:

df['DataFrame Column'] = df['DataFrame Column'].astype(float)

For instance, let’s convert the ‘Prices’ column from integer to float:

import pandas as pd

Data = {'Products': ['AAA','BBB','CCC','DDD','EEE'],
          'Prices': [200,700,400,1200,900]}

df = pd.DataFrame(Data)

df['Prices'] = df['Prices'].astype(float)
print (df['Prices'].dtypes)

Once you run the code, you’ll notice that the data type for the ‘Prices’ column is now float:

Float

You may wish to check the pandas documentation for additional information about df.dtypes.