Related Articles

Related Articles

How to Check the Data Type in Pandas DataFrame?
  • Last Updated : 02 Jul, 2020

Pandas DataFrame is a Two-dimensional data structure of mutable size and heterogeneous tabular data. There are different Built-in data types available in Python.  Two methods used to check the datatypes are pandas.DataFrame.dtypes and pandas.DataFrame.select_dtypes.

Consider an dataset of a shopping store having data about Customer Serial Number, Customer Name, Product ID of the purchased item, Product Cost and Date of Purchase.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#importing pandas as pd
import pandas as pd
  
# Create the dataframe 
df = pd.DataFrame({
'Cust_No': [1,2,3],
'Cust_Name': ['Alex', 'Bob', 'Sophie'],
'Product_id': [12458,48484,11311],
'Product_cost': [65.25, 25.95, 100.99],
'Purchase_Date': [pd.Timestamp('20180917'),
                  pd.Timestamp('20190910'),
                  pd.Timestamp('20200610')]
})
  
# Print the dataframe 
df

chevron_right


Output: 



Method 1: Using pandas.DataFrame.dtypes 

For user to check DataType of particular Dataset or particular column from dataset can use this method. This method return a list of data types for each column or also return just a data type of a particular column

Example 1 : 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Print a list datatypes of all columns
  
df.dtypes

chevron_right


Output:

Example 2: 

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

# print datatype of particular column
df.Cust_No.dtypes

chevron_right


Output: 

dtype('int64')

Method 2: Using pandas.DataFrame.select_dtypes 

Unlike checking Data Type user can alternatively perform check to get the data for particular datatype if it is existing otherwise get an empty dataset in return. This method return a subset of the DataFrame’s columns based on the column dtypes.

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Returns Two column of int64 
df.select_dtypes(include = 'int64')

chevron_right


Output: 

python-padnas

Example 2: 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Returns columns excluding int64 
df.select_dtypes(exclude = 'int64')

chevron_right


Output : 



Example 3 : 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Print an empty list as there is
# no column of bool type
df.select_dtypes(include = "bool")

chevron_right


Output : 


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :