Skip to main content

Command Palette

Search for a command to run...

How to find the count of only numeric columns in Pandas DataFrame?

Updated
View as Markdown
How to find the count of only numeric columns in Pandas DataFrame?
S

I build, write and explore around AI, data, and technology. Sometimes it’s experiments, sometimes it’s just reflections. All of it helps me learn.

Often times we have 100+ columns in the dataset and we have to find out how many columns are numeric and ultimately count of the columns

numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
numeric_df = df.select_dtypes(include=numerics)
len(numeric_df.columns)

Here, the first line creates a list of all the data types having numeric values and saves them into a variable called numerics

then numeric_df variable stores column for elements stated in numerics variable.

Simply, in the last, we are using the simple len function to calculate the number of columns.

M

This would have saved me a lot of code lines! As a newbie in Pandas, didn't know this. Great.

1
S

glad to hear it helped you... thanks 😊