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?
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 typesSyntax:
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
📚 Data Science Programs By Skill Level Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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:
Example 1: Get all numeric columns
# select all numeric columns df.select_dtypes(include='number')
Output:
Example 2: Get all integer columns
# select all integer columns df.select_dtypes(include='int')
Output:
Example 3: Get all float columns
# select all float columns df.select_dtypes(include='float')
Output:
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:
Example 5: Get all numeric columns using _get_numeric_data()
function
# select all numeric columns df._get_numeric_data()
Output:
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 –
- Check if a DataFrame column is of datetime dtype in Pandas
- Check if Pandas DataFrame column has object dtype
- Get Count of dtypes in a Pandas DataFrame
- Pandas – Select Columns of a Specific Type
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.