• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / How to Select Numerical Columns from a Pandas Dataframe

How to Select Numerical Columns from a Pandas Dataframe

April 7, 2020 by cmdlinetips

In this post, we will learn how to use Pandas to select columns based on their datatypes. For example, if we have Pandas dataframe with multiple data types, like numeric and object and we will learn how to select columns that are numeric.

We can use Pandas’ seclect_dtypes() function and specify which data type to include or exclude. This will allow us to select/ ignore columns by their data types.

Let us load Pandas and check its version.

import numpy as np
import pandas as pd
pd.__version__
1.0.0

We will use College Tuition data set from tidytuesday project. We will load the data reading dorectly from tidytuesday’s website.

data_url="https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-10/tuition_cost.csv"
df = pd.read_csv(data_url)
df.iloc[0:5,0:3]

It contains columns corresponding to multiple datatypes. Here is the first few rows of the data frame.

name	state	state_code
0	Aaniiih Nakoda College	Montana	MT
1	Abilene Christian University	Texas	TX
2	Abraham Baldwin Agricultural College	Georgia	GA
3	Academy College	Minnesota	MN
4	Academy of Art University	California	CA

Select Columns that are numeric from Pandas dataframe

If we want to select columns that are integers or doubles (anything numneric), we can use include argument to select_dtypes() function and specify include=’number’ as shown below.


df.select_dtypes(include='number').head()

This excludes any non-numeric columns and gives us only the columns that are numeric.

  room_and_board in_state_tuition in_state_total out_of_state_tuition out_of_state_total
0	NaN	2380	2380	2380	2380
1	10350.0	34850	45200	34850	45200
2	8474.0	4128	12602	12550	21024
3	NaN	17661	17661	17661	17661
4	16648.0	27810	44458	27810	44458

Select Columns that are Non-numeric from Pandas dataframe

Similarly if we wanted to select columns that are non-numeric, i.e. “object”, we can use select_dtype() function with include=’object’.


df.select_dtypes(include='object').head()

And we get columns that are of type “object”.

   name	state	state_code  type  degree_length
0	Aaniiih Nakoda College	Montana	MT	Public	2 Year
1	Abilene Christian University	Texas	TX	Private	4 Year
2	Abraham Baldwin Agricultural College	Georgia	GA	Public	2 Year
3	Academy College	Minnesota	MN	For Profit	2 Year
4	Academy of Art University	California	CA	For Profit	4 Year

How to exclude columns of certain datatypes from Pandas dataframe

We can get the same behaviour from select_dtypes() function, but using the argument exclude instead of include.

For example, to select columns that are non-object, we can use select_dtypes() with exclude=’object’.



df.select_dtypes(exclude='object').head()

In this case this gives us numerical columns by excluding columns that are of type object.

room_and_board	in_state_tuition in_state_total	out_of_state_tuition	out_of_state_total
0	NaN	2380	2380	2380	2380
1	10350.0	34850	45200	34850	45200
2	8474.0	4128	12602	12550	21024
3	NaN	17661	17661	17661	17661
4	16648.0	27810	44458	27810	44458

This post is part of the series on Pandas 101, a tutorial covering tips and tricks on using Pandas for data munging and analysis.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Select Columns by Data Type in Pandas? Default ThumbnailHow To Get The Memory Usage of Pandas Dataframe? How To Select Columns in Python Pandas?How To Select One or More Columns in Pandas? 4 ways to select columns with dplyr select()4 ways to select columns from a dataframe with dplyr’s select()

Filed Under: Pandas 101 Tagged With: Pandas select_dtypes, Select Columns by Data Type, select numerical columns Pandas

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version