0

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
Shital
  • 53
  • 6

2 Answers2

2

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  17  398
1  23  476

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

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
  • Thanks for answering, I want to get all numeric type columns from dataframe. I have edited my que can you please suggest solution? – Shital Sep 14 '22 at 17:52
  • Hi @Shital the provided solution works for the new data as well. – ThePyGuy Sep 15 '22 at 01:22
1

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
Ben Grossmann
  • 4,387
  • 1
  • 12
  • 16
  • Thanks for answering, I want to get all numeric type columns from dataframe. I have edited my que can you please suggest solution? – Shital Sep 14 '22 at 17:58
  • @Shital See [this post](https://stackoverflow.com/questions/25039626/how-do-i-find-numeric-columns-in-pandas) – Ben Grossmann Sep 14 '22 at 18:18