Home > Software engineering >  How to get only numeric type columns from dataframe
How to get only numeric type columns from dataframe

Time:09-15

I want to get only numeric type columns from dataframe. Can anyone please suggest any solution for this?

Ex:

A    B    C
17   398  file
23   476  name

O/p:

A    B
17   398
23   476

CodePudding user response:

Pandas DataFrame has select_dtypes method that allows you to select the columns based on the data type.

>>> df.select_dtypes(int)
   A  B
0  1  3
1  2  4

You can also pass a sequence for multiple data types for e.g.: df.select_dtypes((int, float)) which selects all integer, and float value columns.

There are other ways as well around it like converting to numeric then discarding all-NaN columns, but this seems to be the best solution for the data you've mentioned

CodePudding user response:

The following works

import numpy as np
import pandas as pd 

df = pd.DataFrame({'float_col': [1.0],
                   'int_col': [1],
                   'datetime_col': [pd.Timestamp('20180310')],
                   'string_col': ['foo']})
df_int = df[df.columns[df.dtypes == np.int64]]

In this case, df is the data frame

   float_col  int_col datetime_col string_col
0        1.0        1   2018-03-10        foo

and df_int is the data frame

   int_col
0        1
  • Related