How to get Datatypes of Columns in Pandas DataFrame?


Pandas DataFrame - Get Datatypes of Columns

To get the datatypes of columns in a Pandas DataFrame, call DataFrame.dtypes property. The DataFrame.dtypes property returns an object of type pandas.Series with the datatypes of each column in it.

Syntax

The syntax to use dtypes property of a DataFrame is

DataFrame.dtypes

Examples

1. Get the datatypes of columns in the given DataFrame

In the following program, we have created a DataFrame with specific data and column names. Let us get the datatypes of columns in this DataFrame using DataFrame.dtypes.

Python Program

import pandas as pd

df = pd.DataFrame(
    [['abc', 22],
    ['xyz', 25],
    ['pqr', 31]],
    columns=['name', 'age'])

datatypes = df.dtypes
print(datatypes)

Output

name    object
age      int64
dtype: object

We can print the elements of the returned value by DataFrame.dtypes using a for loop as shown in the following.

Python Program

import pandas as pd

df = pd.DataFrame(
    [['abc', 22],
    ['xyz', 25],
    ['pqr', 31]],
    columns=['name', 'age'])

datatypes = df.dtypes
for dtype in datatypes:
    print(dtype)

Output

object
int64

Summary

In this tutorial of Python Examples, we learned how to get the datatypes of column in a DataFrame using DataFrame.dtypes property.