Home > Net >  casting dataframe to_numpy array removes decimal number but leaves the decimal period
casting dataframe to_numpy array removes decimal number but leaves the decimal period

Time:08-24

Hello people of the web,

I am trying to transform a dataframe's column into a numpy array, yet I find myself having a weird issue I couldn't verbalise good enough in order to find any information to handle it.

The original dataframe: enter image description here

The column 'height' is the one i'm trying to transform (the rightest column in the df) using to_numpy function, in the following way:

enter image description here

gives me the following result: enter image description here

as you can notice in the 3rd image, to_numpy() has erased the rightest digit (the 0) from the heights while leaving the decimal period in place..

I have tried using astype(float) without success, in the following ways:

data.iloc[:, size_df].astype(float).to_numpy()
data.iloc[:, size_df].to_numpy().astype(float)

thanks in advance.

CodePudding user response:

So, as @hpaulj mentioned, it's just a representation of the same number. Following his advice, I printed the datatype, and I could see that it is still considered as a float:

print(data.iloc[:, size_df].to_numpy().dtype) // prints 'float64'

In order to get rid of the decimal point, I could cast, as @sj95126 suggested, to integer, by using .to_numpy(dtype=int)

Thanks!

CodePudding user response:

Follo @dani_l_n advice. Also, in case of numpy it would be better to cast to numpt dtypes rather than python data types like np.int32, etc.

  • Related