The select_dtypes()
function in pandas
returns a subset of a given DataFrame's columns based on the column data types.
The select_dtypes()
function's syntax is shown below:
DataFrame.select_dtypes(include=None, exclude=None)
The select_dtypes()
function takes any of the two parameter values: include
and exclude
(at least one of them must be supplied). These represent the selection of the data type(s) to be included or excluded.
The select_dtypes()
function returns a subset of the DataFrame that includes the data types in include
and excludes the data types in exclude
.
# A code to illustrate the select_dtypes() function in Pandas# importing the pandas libraryfrom pandas import DataFrame# creating a dataframemy_data_frame = DataFrame({'Id': [1, 2, 3, 4, 5, 6],'Married': [True, False] * 3,'Score': [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]})# obtaining the columns by their data typesbool_data = my_data_frame.select_dtypes(include=bool)int_and_float_data = my_data_frame.select_dtypes(include=[int, float])# obtaining the columns by excluding data typesdata_without_int_values = my_data_frame.select_dtypes(exclude=int)# printing resultsprint(bool_data)print(int_and_float_data)print(data_without_int_values)
DataFrame
from the pandas
library.DataFrame
object, my_data_frame
.select_dtypes()
function. We assign the results to the bool_data
, int_and_float_data
and data_without_int_values
variables.bool_data
, int_and_float_data
and data_without_int_values
.