Convert Float to Int on specific columns in a data frame

I am trying to convert columns 0 to 4 and 6 to int from existing float types.

I tried:

df[0:4,6].astype(int)

but of course this does not work ...

+4
source share
1 answer

consider df

df = pd.DataFrame(np.random.rand(10, 10) * 10)

enter image description here

use np.r_to getslc

slc = np.r_[0:4, 6]
df[slc] = df[slc].astype(int)
df

or pass a type dictionary with keys as column names

df.astype({c: int for c in slc})

enter image description here

+7
source

Source: https://habr.com/ru/post/1662205/


All Articles