Pandas DataFrame | select_dtypes method
Start your free 7-days trial now!
Pandas DataFrame.select_dtypes(~)
returns a subset of columns that match (or not match) the specified type.
Parameters
1. include
| scalar
or array-like
| optional
The data-types to include.
2. exclude
| scalar
or array-like
| optional
The data-types to exclude.
At least one of two parameters must be provided.
Here are some of the data-types you can specify:
Type | Description |
---|---|
| Matches all numeric types like |
| Matches all floats |
| Matches all ints |
| Matches all objects such as strings, lists, dictionaries and so on |
| Matches all datetimes |
| Matches all timedeltas |
| Matches all categories |
| Matches all booleans |
Return value
A DataFrame holding a subset of columns that match (or not match) the specified type.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[4,5], "B":["M","L"], "C": [8,9]}, index=["a","b"])df
A B Ca 4 M 8b 5 L 9
To fetch all numeric columns:
df.select_dtypes(include="number")
A Ca 4 8b 5 9
To fetch all columns except numeric ones:
df.select_dtypes(exclude="number")
Ba Mb L