Which pandas
methods can be used to get names of the columns of a given DataFrame
that have numeric dtypes
(of all sizes, such as uint8
, and not just 64-bit ones), using a single line of code? Note: in practice there are hundreds of columns, so dtypes
(or another vectorized method) should be used for detecting data types.
import numpy as np
import pandas as pd
test_df = pd.DataFrame(data=[{"str_col": "some string",
"int_col": 0,
"float_col": 3.1415}])
test_df.dtypes[test_df.dtypes == np.dtype('float')].index.values[0]
# "float_col"
test_df.dtypes[test_df.dtypes == np.dtype('int')].index.values[0]
# "int_col"
# ?
# ["float_col", "int_col"]
To get the numerical columns:
numeric_cols = test_df.select_dtypes(include=['number']).columns.tolist()
Result:
['int_col', 'float_col']