Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get names of all numeric columns in a pandas DataFrame (filter by dtype)

Which pandas methods can be used to get names of the columns of a given DataFrame that have numeric dtypes (of all sizes, 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"]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To get the numerical columns:

numeric_cols = test_df.select_dtypes(include=['number']).columns.tolist()

Result:

['int_col', 'float_col']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading