I am trying to convert columns 0 to 4 and 6 to ints from there current float types.
I tried:
df[0:4,6].astype(int)
but of course this does not work...
I was getting an error as some of my column values were NaN
which obviously can not be converted to int
. So a better approach would be to handle NaN
before converting the datatype
and avoid ValueError: Cannot convert non-finite values (NA or inf) to integer
.
df['col_name'] = df['col_name'].fillna(0).astype(int)
This fills NaN
with 0
and then converts to the desired datatype
which is int
in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With