How do I find numeric columns in Pandas?
Simple one-line answer to create a new dataframe with only numeric columns:
df.select_dtypes(include=np.number)
If you want the names of numeric columns:
df.select_dtypes(include=np.number).columns.tolist()
Complete code:
import pandas as pdimport numpy as npdf = pd.DataFrame({'A': range(7, 10), 'B': np.random.rand(3), 'C': ['foo','bar','baz'], 'D': ['who','what','when']})df# A B C D# 0 7 0.704021 foo who# 1 8 0.264025 bar what# 2 9 0.230671 baz whendf_numerics_only = df.select_dtypes(include=np.number)df_numerics_only# A B# 0 7 0.704021# 1 8 0.264025# 2 9 0.230671colnames_numerics_only = df.select_dtypes(include=np.number).columns.tolist()colnames_numerics_only# ['A', 'B']
You can use the undocumented function _get_numeric_data()
to filter only numeric columns:
df._get_numeric_data()
Example:
In [32]: dataOut[32]: A B0 1 s1 2 s2 3 s3 4 sIn [33]: data._get_numeric_data()Out[33]: A0 11 22 33 4
Note that this is a "private method" (i.e., an implementation detail) and is subject to change or total removal in the future. Use with caution.