You can use the following methods to select columns in a pandas DataFrame that are equal to a specific data type:
Method 1: Select Columns Equal to Specific Data Type
#select all columns that have an int or float data type
df.select_dtypes(include=['int', 'float'])
Method 2: Select Columns Not Equal to Specific Data Type
#select all columns that don't have a bool or object data type
df.select_dtypes(exclude=['bool', 'object'])
The following examples show how to use each method with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F'], 'points': [18, 22, 19, 14, 14, 11], 'assists': [5, 7, 7, 9, 12, 9], 'minutes': [10.1, 12.0, 9.0, 8.0, 8.4, 7.5], 'all_star': [True, False, False, True, True, True]}) #view DataFrame print(df) team points assists minutes all_star 0 A 18 5 10.1 True 1 B 22 7 12.0 False 2 C 19 7 9.0 False 3 D 14 9 8.0 True 4 E 14 12 8.4 True 5 F 11 9 7.5 True
Example 1: Select Columns Equal to Specific Data Type
We can use the following code to select all columns in the DataFrame that have a data type equal to either int or float:
#select all columns that have an int or float data type
df.select_dtypes(include=['int', 'float'])
points assists minutes
0 18 5 10.1
1 22 7 12.0
2 19 7 9.0
3 14 9 8.0
4 14 12 8.4
5 11 9 7.5
Notice that only the columns with a data type equal to int or float are selected.
Example 2: Select Columns Not Equal to Specific Data Type
We can use the following code to select all columns in the DataFrame that do not have a data type equal to either bool or object:
#select all columns that don't have a bool or object data type
df.select_dtypes(exclude=['bool', 'object'])
points assists minutes
0 18 5 10.1
1 22 7 12.0
2 19 7 9.0
3 14 9 8.0
4 14 12 8.4
5 11 9 7.5
Notice that only the columns that don’t have a data type equal to bool or object are selected.
Also note that you can use the following syntax to display the data type of each column in the DataFrame:
#display data type of all columns
df.dtypes
team object
points int64
assists int64
minutes float64
all_star bool
dtype: object
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
Pandas: How to Check dtype for All Columns in DataFrame
Pandas: Get Index of Rows Whose Column Matches Value
Pandas: How to Set Column as Index