Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to Check the Data Type in Pandas DataFrame?

How to Check the Data Type in Pandas DataFrame?

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.

Creating a Dataframe to Check DataType in Pandas DataFrame

Consider a 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




#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


Output: 

 

Check the Data Type in Pandas using pandas.DataFrame.dtypes 

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

Example 1:

Python3




# Print a list datatypes of all columns
 
df.dtypes


 Output:

 

Example 2:

Python3




# print datatype of particular column
df.Cust_No.dtypes


Output: 

dtype('int64')

Example 3:

Python3




# Checking the Data Type of a Particular Column
df['Product_cost'].dtypes


Output: 

dtype('float64')

Check the Data Type in Pandas using pandas.DataFrame.select_dtypes 

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

Example 1:

Python3




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


Output: 

 

Example 2: 

Python3




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


Output : 

 

Example 3 : 

Python3




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


Output : 

 

RELATED ARTICLES

Most Popular

Recent Comments