How to select the columns in a DataFrame based on the data type

In this shot, we will discuss how we can select columns based on their data types in pandas.

Let’s first create a DataFrame.

import pandas as pd
drinks = pd.read_csv('http://bit.ly/drinksbycountry')
drinks = drinks[["country",
"beer_servings",
"wine_servings",
"continent"
]]
print("Datatypes of the columns:\n", drinks.dtypes)

Explanation

  • In line 1, we import the required package.
  • In line 3, we read the data into a DataFrame and name it drinks.
  • In line 4, we take only four columns for readability purposes.
  • In line 9, we print the data type of each column present in our DataFrame.

Now, we will use some data types to filter the columns.

number_cols = drinks.select_dtypes(include='number').head()
print("Number Columns:\n",number_cols)
object_cols = drinks.select_dtypes(include='object').head()
print("\nObject Columns:\n",object_cols)
multi_cols = drinks.select_dtypes(include=['number', 'object']).head()
print("\nMulti-Datatype Columns:\n", multi_cols)
exclude_cols = drinks.select_dtypes(exclude='number').head()
print("\nExcluded Datatype Columns:\n",exclude_cols)

Explanation

  • In line 1, we use the function select_dtypes() and pass the data type as number, which will filter the columns with their data type as number.
  • In line 2, we print the filtered data.
  • In line 4, we again use the function select_dtypes(), but this time we pass the data type as object.
  • In line 5, we print the filtered data.
  • In line 7, we use the select_dtypes() function, but we pass a list of data types that we want to use for filtering the columns.
  • In line 8, we print the filtered data.
  • In line 10, we use the same function, but this time, instead of passing the data type on which we want to filter the columns, we specify the data type that we want to exclude from this filter (i.e., we apply the filter that will select all the columns that are not of the type exclude = number).
  • In line 11, we print the filtered data.

Free Resources