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
import pandas as pd
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' )]
})
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:
Output:
Example 2:
Output:
dtype('int64')
Example 3:
Python3
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
df.select_dtypes(include = 'int64' )
|
Output:
Example 2:
Python3
df.select_dtypes(exclude = 'int64' )
|
Output :
Example 3 :
Python3
df.select_dtypes(include = "bool" )
|
Output :
Last Updated :
08 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...