In Pandas, the select_dtypes()
function is used to return the subset of the columns of a dataframe by specifying the data types.
Some datatypes or dtype
in Python include float64
, bool
, int64
, and more.
DataFrame.select_dtypes(include=None, exclude=None)
This function takes the following parameter values:
include
: This is used to specify the datatype to be included or returned in the output result.exclude
: This is used to specify the datatype to be excluded in the output result.Note: At least one of the parameters,
include
orexclude
, must be passed to theselect_dtypes()
function.
This function returns the subset of the given dataframe having the datatypes specified in include
and excluding the datatypes in exclude
.
import pandas as pd# creating a DataFramedf = pd.DataFrame({'INTEGERS': [1, 0] * 3,'BOOLEAN': [True, False] * 3,'FLOAT': [1.0, 2.0] * 3})# printing the DataFrameprint(df)# implementing the select_dtypes() function to include boolean valuesprint(df.select_dtypes(include="bool"))# implementing the select_dtypes() function to exclude boolean valuesprint(df.select_dtypes(exclude="bool"))
pandas
module.df
.df
.select_dtypes()
function to include boolean values. We print the results to the console.select_dtypes()
function to exclude boolean values. We print the results to the console.