How to find numeric columns in Pandas?


To find numeric columns in Pandas, we can make a list of integers and then include it into select_dtypes() method. Let's take an example and see how to apply this method.

Steps

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
  • Print the input DataFrame, df.
  • Make a list of data type, i.e., numerics, to select a column.
  • Return a subset of the DataFrame's columns based on the column dtypes.
  • Print the column whose data type is int.

Example

import pandas as pd

df = pd.DataFrame(
   dict(
      name=['John', 'Jacob', 'Tom', 'Tim', 'Ally'],
      marks=[89, 23, 100, 56, 90],
      subjects=["Math", "Physics", "Chemistry", "Biology", "English"]
   )
)

print "Input DataFrame is:\n", df
numerics = ['int16', 'int32', 'int64']
df = df.select_dtypes(include=numerics)
print "Numeric column in input DataFrame is:\n", df

Output

Input DataFrame is:
    name   marks   subjects
0   John    89         Math
1  Jacob    23      Physics
2    Tom   100    Chemistry
3    Tim    56      Biology
4   Ally    90      English

Numeric column in input DataFrame is:
    marks
0    89
1    23
2   100
3    56
4    90

Updated on: 15-Sep-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements