How do I select all the numeric or text columns in pandas?
Short answer
Use select_dtypes with include or exclude to pick columns by type, such as all numeric columns. It is more robust than listing column names by hand, which breaks when the data changes.
The short version
Use select_dtypes(include="number") to get every numeric column, or include="object" for text. Pass exclude to do the opposite. This adapts automatically as the data changes, whereas a hand-written list of column names silently goes stale.
Selecting by type
import pandas as pd
df = pd.DataFrame({
"quantity": [1],
"city": ["Leeds"],
"price": [1.5],
})
df.select_dtypes(include="number").columns.tolist() # ['quantity', 'price']
df.select_dtypes(include="object").columns.tolist() # ['city']
"number" covers both integers and floats, which is usually what you mean by numeric.
Why not just list the columns
numeric = df[["quantity", "price"]] # breaks when a column is added or renamed
The moment a new numeric column arrives, this silently misses it, and your summary is quietly incomplete. select_dtypes always reflects what is actually there.
Applying an operation to every numeric column
The natural pairing is with a transformation you want on all numbers.
numeric_cols = df.select_dtypes(include="number").columns
df[numeric_cols] = df[numeric_cols].fillna(0)
This fills the numeric gaps without touching text columns, where a zero would make no sense.
Excluding instead
df.select_dtypes(exclude="number") # everything that is not numeric
Handy for finding the columns that need cleaning, since text and object columns are where messy values hide.
Common type names
Use "number", "object" (text and mixed), "bool", "category" and "datetime". You can pass a list to combine them.
df.select_dtypes(include=["number", "bool"])
The object-dtype warning
A column of numbers stored as text has dtype object, so it will not be picked up by include="number". If a column you expected to be numeric is missing from the selection, that is a useful signal: it is still text, probably because of a stray character, and needs converting with pd.to_numeric first.
df.dtypes # check what pandas actually thinks each column is
This is one of the quiet ways select_dtypes earns its keep: it tells you not just what is numeric, but what you thought was numeric and is not.
In a pipeline
Because it returns a normal frame, it slots into a chain, which makes it easy to summarise just the numbers.
df.select_dtypes("number").describe()