How To Get All Numeric Columns In Pandas


Pandas is a powerful data manipulation library in Python that provides various functions and methods to work with structured data. One common task when working with data is to identify and extract numeric columns for further analysis. In this article, we will explore different techniques to get all numeric columns in Pandas.

Understanding the Dataset

Before we dive into the methods, let’s first understand the dataset we will be working with. For this tutorial, we will use a sample dataset containing information about sales transactions. It includes columns such as transaction ID, customer ID, product ID, quantity, price, and total amount.

Method 1: Using dtypes

One simple way to get all numeric columns in Pandas is by using the dtypes attribute. This attribute returns the data type of each column in the DataFrame. We can filter out the numeric columns by selecting the columns with data type ‘int64’ or ‘float64’.

Here is an example:

“`python numeric_columns = df.select_dtypes(include=[‘int64’, ‘float64’]) “` This will return a new DataFrame containing only the numeric columns from the original dataset.

Method 2: Using describe

Another approach is to use the describe method, which provides summary statistics for each column. By default, it only includes numerical columns. We can extract the column names from the describe output and select those columns from the original DataFrame.

Here is an example:

“`python summary = df.describe() numeric_columns = df[summary.columns] “` This method is useful when we need both the summary statistics and the numeric columns in a single step.

Handling Missing Values

In real-world datasets, missing values are a common occurrence. It is essential to handle them appropriately before performing any analysis. Pandas provides several methods to deal with missing values, such as dropna and fillna.

Method 3: Dropping Missing Values

If we want to exclude rows with missing values from our analysis, we can use the dropna method. By default, it drops any row containing at least one missing value. We can apply this method to the numeric columns DataFrame obtained from the previous methods.

Here is an example:

“`python numeric_columns_without_missing = numeric_columns.dropna() “` This will create a new DataFrame without any missing values in the numeric columns.

Method 4: Filling Missing Values

Alternatively, we can fill the missing values with a specific value using the fillna method. This is useful when we want to preserve the rows but replace the missing values with a meaningful value, such as the column mean or median.

Here is an example:

“`python numeric_columns_filled = numeric_columns.fillna(numeric_columns.mean()) “` This will fill the missing values in the numeric columns with their respective means.


In this article, we explored different techniques to get all numeric columns in Pandas. We learned how to use the dtypes attribute, describe method, and handle missing values in the numeric columns. These methods are essential for data analysis tasks that require numerical data. By applying these techniques, you can efficiently extract and work with the numeric columns in your datasets.

You May Also Like