In this article we will discuss different ways to fetch the data type of single or multiple columns. Also see how to compare data types of columns and fetch column names based on data types.

Use Dataframe.dtypes to get Data types of columns in Dataframe

In Python’s pandas module Dataframe class provides an attribute to get the data type information of each columns i.e.

It returns a series object containing data type information of each column. Let’s use this to find & check data types of columns.

Suppose we have a Dataframe i.e.

Contents of the dataframe are,

Let’s fetch the Data type of each column in Dataframe as a Series object,

Output

Index of returned Series object is column name and value column of Series contains the data type of respective column.

Get Data types of Dataframe columns as dictionary

We can convert the Series object returned by Dataframe.dtypes to a dictionary too,

Output:

Get the Data type of a single column in Dataframe

We can also fetch the data type of a single column from series object returned by Dataframe.dtypes i.e.

Output

Check if data type of a column is int64 or object etc.

Using Dataframe.dtypes we can fetch the data type of a single column and can check its data type too i.e.

Check if Data type of a column is int64 in Dataframe

Output

Check if Data type of a column is object i.e. string in Dataframe

Output

Get list of pandas dataframe column names based on data type

Suppose we want a list of column names whose data type is np.object i.e string. Let’s see how to do that,

Output

We basically filtered the series returned by Dataframe.dtypes by value and then fetched index names i.e. columns names from this filtered series.

Get data types of a dataframe using Dataframe.info()

Dataframe.info() prints a detailed summary of the dataframe. It includes information like

  • Name of columns
  • Data type of columns
  • Rows in dataframe
  • non null entries in each column

Let’s see an example,

Output

It also gives us detail about data types of columns in our dataframe.

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.