Skip to Content

Pandas – Get All Numeric Columns

In this tutorial, we will look at how to get the numeric columns in a Pandas Dataframe.
Later, we will understand the same with the help of a few examples.

How to get the numeric columns in a Pandas Dataframe?

select all numeric columns from a pandas dataframe

We can get numeric type columns in a Pandas Dataframe by:

  • The pandas select_dtypes() method
  • The pandas _get_numeric_data() method

1. select_dtypes() method:
We can use the .select_dtypes() method to select columns from a dataframe using their dtypes. You can select columns in different ways as shown below:

1.1 include='number' includes all numpy number data types

Syntax: dataFrameName.select_dtypes(include='number')

1.2 passing the data type as argument

Syntax: dataFrameName.select_dtypes(include='dataType')

1.3 making a custom list of data types and then passing it as an argument

Syntax:

numericlist = ['int_','int8','int16', 'int32', 'int64' ...]

dataFrameName.select_dtypes(include = numericlist)

2. _get_numeric_data() method:

We can get numeric type columns in a Pandas Dataframe, using the dataframe _get_numeric_data() method as shown in the syntax below.

Syntax: dataFrameName._get_numeric_data()

Examples

We will now look at a few examples for a better understanding.

But before that, we will create a pandas dataframe that we will be using throughout this tutorial using the following command:

import pandas as pd

# employee data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32],
    "Salary": [70000.00, 80000.00, 72000.50, 66000.00],
    "Department": ["Sales", "Sales", "Accounting", "HR"]
}

# create pandas dataframe
df = pd.DataFrame(data)
# displays dataframe
df

Output:

the pandas dataframe with employee information

Example 1: Get all numeric columns

# select all numeric columns
df.select_dtypes(include='number') 

Output:

dataframe with only numeric columns

Example 2: Get all integer columns

# select all integer columns
df.select_dtypes(include='int') 

Output:

only integer columns selected from the dataframe

Example 3: Get all float columns

# select all float columns
df.select_dtypes(include='float')

Output:

only float type column selected from the dataframe

Example 4: Get columns based on a custom dtype list

# list with dtypes to include
numeric_list = ['int64', 'float'] 
# select columns based on the above list
df.select_dtypes(include=numeric_list)

Output:

dataframe with only numeric columns

Example 5: Get all numeric columns using _get_numeric_data() function

# select all numeric columns
df._get_numeric_data() 

Output:

dataframe with only numeric columns

Summary

In this tutorial, we looked at how to get numeric columns in a Pandas dataframe. The following are the key takeaways –

  • Use the pandas select_dtypes() method by specifying the dtypes of the columns to include.
  • Use the pandas ._get_numeric_data() method directly on the dataframe to get all the numeric type columns.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.