Skip to main content

Select all numeric types from Pandas DataFrame in Python

To extract numeric types from Pandas DataFrame, Use select_types method.
DataFrame.select_dtypes(include=None, exclude=None)
import numpy as np
import pandas as pd
df = pd.DataFrame({'a': [1, 2] * 3,
                   'b': [True, False] * 3,
                   'c': [1.0, 2.0] * 3,
                   'd': ['one','two'] * 3})
To select all numeric types, set the parameter 'include' to np.number or 'number'.
df.select_dtypes(include='number')
df.select_dtypes(include=np.number)
   a    c
0  1  1.0
1  2  2.0
2  1  1.0
3  2  2.0
4  1  1.0
5  2  2.0
You can also use the Python built-in types such as int and float, or 'int' and 'float' as string.
df.select_dtypes(include=int)
df.select_dtypes(include='int')
   a
0  1
1  2
2  1
3  2
4  1
5  2
df.select_dtypes(include=float)
df.select_dtypes(include='float')
     c
0  1.0
1  2.0
2  1.0
3  2.0
4  1.0
5  2.0
To select strings you must use the object dtype.
df.select_dtypes(include=object)
     d
0  one
1  two
2  one
3  two
4  one
5  two
Multiple data types can be specified in the list.
df.select_dtypes(include=[int, float])
   a    c
0  1  1.0
1  2  2.0
2  1  1.0
3  2  2.0
4  1  1.0
5  2  2.0

Comments