1

I'm creating a datatable as follows,

spotify_songs_dt = dt.fread('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-21/spotify_songs.csv')

and its column types are,

spotify_songs_dt.stypes

Here I would like to take out only numeric fields of DT, and how can it be achieved in a datatable way?. In pandas dataframe we have a kind of function select_dtypes() for it.

Pasha
  • 6,298
  • 2
  • 22
  • 34
myamulla_ciencia
  • 1,282
  • 1
  • 8
  • 30

1 Answers1

2

If you have a frame DT, then the most straightforward way to select columns of a specific type is to use the type itself in the DT[:,j] selector:

DT[:, bool]          # all boolean columns
DT[:, int]           # all integer columns
DT[:, float]         # all floating columns
DT[:, str]           # string columns
DT[:, dt.int32]      # columns with stype int32
DT[:, dt.ltype.int]  # columns with ltype `int`, same as DT[:, int]

It is also possible to provide a list of types to select:

DT[:, [int, float]]          # integer and floating columns
DT[:, [dt.int32, dt.int64]]  # int32 and int64 columns

Sometimes it may also be useful to delete the columns of the undesirable type instead of selecting the ones you need:

del DT[:, str]
Pasha
  • 6,298
  • 2
  • 22
  • 34