Calculate metrics on numeric columns only for Pandas DataFrames

TL, DR

Sometimes you need to calculate averages, minimums, or other metrics from numeric-only columns in a Pandas DataFrame. Here a few snippets to do it.

Select numeric columns and calculate metrics

You often want to calculate summary statistics like the mean , median , or standard deviation — but you only care about numeric columns.

Trying to apply these functions directly to mixed-type DataFrames can cause issues or return less useful results. Instead, use .select_dtypes() to filter numeric columns before computing:

numeric_mean = df.select_dtypes(include='number').mean()

Here’s what’s happening:

  • select_dtypes(include='number'): selects only numeric columns (int, float, etc.)
  • .mean(): calculates the mean across those numeric columns

You can easily swap .mean() with .median(), .std(), or .sum() depending on your needs.

Related links

  • Pandas DataFrame.select_dtypes() documentation link

Do you like our content? Check more of our posts in our blog!