Selection by data type
So far in this cookbook, we have seen data types, but we have not talked too much in depth about what they are. We still aren’t quite there; a deep dive into the type system of pandas is reserved for Chapter 3, Data Types. However, for now, you should be aware that the column type provides metadata that pd.DataFrame.select_dtypes can use for selection.
How to do it
Let’s start with a pd.DataFrame that uses integral, floating point, and string columns:
df = pd.DataFrame([
[0, 1.0, "2"],
[4, 8.0, "16"],
], columns=["int_col", "float_col", "string_col"])
df
int_col float_col string_col
0 0 1.0 2
1 4 8.0 16
Use pd.DataFrame.select_dtypes to select only integral columns:
df.select_dtypes("int")
int_col
0 0
1 4
Multiple types can be selected if you pass a list argument:
df.select_dtypes(include...