How to Check the Data Type in Pandas DataFrame

To check the data type of all columns in Pandas DataFrame:

df.dtypes

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: Create a DataFrame

To start, create a DataFrame with 3 columns:

import pandas as pd

data = {
    'products': ['Product A', 'Product B', 'Product C'],
    'prices': [100, 250, 875],
    'sold_date': ['2023-11-01', '2023-11-03', '2023-11-05']
}

df = pd.DataFrame(data)

print(df)

Run the script in Python, and you’ll get the following DataFrame:

    products  prices   sold_date
0  Product A     100  2023-11-01
1  Product B     250  2023-11-03
2  Product C     875  2023-11-05

Step 2: Check the Data Type

You can now check the data type of all columns in the DataFrame by adding df.dtypes to the script:

df.dtypes

Here is the complete Python script:

import pandas as pd

data = {
    'products': ['Product A', 'Product B', 'Product C'],
    'prices': [100, 250, 875],
    'sold_date': ['2023-11-01', '2023-11-03', '2023-11-05']
}

df = pd.DataFrame(data)

print(df.dtypes)

You’ll notice that the data type for the “products” and “sold_date” columns is Object which represents strings. While the data type for the “prices” column is integer:

products     object
prices        int64
sold_date    object

To convert the “sold_date” column from an object (string) to a datetime:

import pandas as pd

data = {
    'products': ['Product A', 'Product B', 'Product C'],
    'prices': [100, 250, 875],
    'sold_date': ['2023-11-01', '2023-11-03', '2023-11-05']
}

df = pd.DataFrame(data)

# Convert the sold_date column to datetime type
df['sold_date'] = pd.to_datetime(df['sold_date'])

print(df.dtypes)

The result:

products             object
prices                int64
sold_date    datetime64[ns]

Checking the Data Type for a Particular Column in Pandas DataFrame

To check the data type for a particular column (e.g., the ‘prices’ column) in the DataFrame:

df['DataFrame Column'].dtypes

Here is the full syntax for our example:

import pandas as pd

data = {
    'products': ['Product A', 'Product B', 'Product C'],
    'prices': [100, 250, 875],
    'sold_date': ['2023-11-01', '2023-11-03', '2023-11-05']
}

df = pd.DataFrame(data)

print(df['prices'].dtypes)

The data type for the ‘prices’ column is integer:

int64

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

You may then use the following template to perform the conversion:

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

For instance, to convert the ‘prices’ column from integer to float:

import pandas as pd

data = {
    'products': ['Product A', 'Product B', 'Product C'],
    'prices': [100, 250, 875],
    'sold_date': ['2023-11-01', '2023-11-03', '2023-11-05']
}

df = pd.DataFrame(data)

# Convert the prices column to float type
df['prices'] = df['prices'].astype(float)

print(df['prices'].dtypes)

The data type for the ‘prices’ column is now float:

float64

You may wish to check the Pandas Documentation for additional information about df.dtypes.

Leave a Comment