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)
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)
select_dtypes()and pass the data type as
number, which will filter the columns with their data type as number.
select_dtypes(), but this time we pass the data type as object.
select_dtypes()function, but we pass a list of data types that we want to use for filtering the columns.
exclude = number).
View all Courses