Explain Codes LogoExplain Codes Logo

Get list of pandas dataframe columns based on data type

python
dataframe
pandas
data-types
Anton ShumikhinbyAnton Shumikhin·Jan 27, 2025
TLDR

Quickly grab columns by data type in a pandas DataFrame using select_dtypes. For instance, fetch all float columns as follows:

# Here we go, fishing for floats! float_cols = df.select_dtypes('float').columns

Combine different types like int and bool in a similar fashion:

# Behold, a beautiful bouquet of ints and bools! int_bool_cols = df.select_dtypes(['int', 'bool']).columns

Another reliable way to filter is through dtypes:

# Numeric types only, please. We're keeping things strictly professional here. numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64'] numeric_cols = df.columns[df.dtypes.isin(numerics)]

To add a layer of customization and cater to dynamic type selection, you can wrap this in a function:

def get_columns_by_type(df, data_types): """ Don't we all love to pick favorites? This function lets you choose your favorite data types! """ return df.select_dtypes(include=data_types).columns.tolist()

On-demand selection with user-friendly functions

Flexible type selection with a function

Advancing from ad-hoc filters, consider capturing the selection process in a utility function for ease of use and better reusability:

# Like a chameleon, this function adapts based on the data types you feed it. def select_columns_by_dtype(df, data_types): return df.select_dtypes(include=data_types).columns.tolist()

Operating directly with dtypes

For more control, you can even operate with the dtypes property directly:

# Now witness the firepower of this fully armed and operational data type dictionary! dtype_dict = df.dtypes.apply(lambda x: x.name).to_dict()

Leveraging the power of masks

To manipulate columns of specific types, a boolean mask can come in handy:

# It's like a mask, but for data. Ninja-style data manipulation — activate! mask = df.dtypes.isin(['int64', 'float64']) selected_cols = df.columns[mask]

Use this mask to pull off type-specific operations:

# Round 'em up, cowboy! Pun totally intended. df.loc[:, mask] = df.loc[:, mask].round(2)

Advanced techniques with dtype groups & conversions

Grouping by dtypes

We can use the groupby feature with dtypes for a grouped view of data types:

# It's like high school all over again — everyone's hanging out with their own group! grouped_dtypes = df.columns.to_series().groupby(df.dtypes).groups

This results in a neatly packed dictionary housing a group-wise distribution of column lists.

for dtype, columns in grouped_dtypes.items(): # Apply mass group operations here like sending a group text (but more fun)

Converting column data types

Sometimes, it's just simpler to convert column types:

# The column's going through a phase and wants to be a 'float'. We respect its choice here. df['some_column'] = df['some_column'].astype('float')