Pandas DataFrame select_dtypes() Method with examples

Spread the love

Pandas select_dtypes() method returns a subset of the DataFrame’s columns based on the column dtypes. You can include or exclude columns based on their dtypes.

Let’s read a dataset.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv'
df = pd.read_csv(url)
df.head()

We can check the data types of each columns using the dtype method.

df.dtypes

Including Columns –

Let’s say that you want to select all integer columns from your dataframe.

df.select_dtypes(include=['int64'])

Excluding Columns –

You can also exclude columns using the exclude parameter. Let’s say you want to exclude all integer and float columns.

df.select_dtypes(exclude=['int64','float64'])

Rating: 1 out of 5.

Leave a Reply