Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting some columns of a matrix from float to int

Tags:

python

numpy

I have a matrix tempsyntheticGroup2 with 6 columns. I want to change the value of columns (0,1,2,3,5) from float to int. This is my code:

tempsyntheticGroup2=tempsyntheticGroup2[:,[0,1,2,3,5]].astype(int)

but it doesn't work properly and I loose the other columns.

like image 226
Talia Avatar asked Oct 29 '15 02:10

Talia


People also ask

How do you convert all float columns to INT in pandas?

To convert a column that includes a mixture of float and NaN values to int, first replace NaN values with zero on pandas DataFrame and then use astype() to convert. Use DataFrame. fillna() to replace the NaN values with integer value zero.

How do I convert a column to an int in Python?

The best way to convert one or more columns of a DataFrame to numeric values is to use pandas.to_numeric() . This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

How do I convert an entire column to numeric in R?

To convert columns of an R data frame from integer to numeric we can use lapply function. For example, if we have a data frame df that contains all integer columns then we can use the code lapply(df,as. numeric) to convert all of the columns data type into numeric data type.


1 Answers

I don't think you can have a numpy array with some element that are ints, and some that are floats (there is only one possible dtype per array). But if you just want to round to lower integer (while keeping all elements as floats) you can do this:

# define dummy example matrix
t = np.random.rand(3,4) + np.arange(12).reshape((3,4))

array([[  0.68266426,   1.4115732 ,   2.3014562 ,   3.5173022 ],
       [  4.52399807,   5.35321628,   6.95888015,   7.17438118],
       [  8.97272076,   9.51710983,  10.94962065,  11.00586511]])



# round some columns to lower int
t[:,[0,2]] = np.floor(t[:,[0,2]])
# or
t[:,[0,2]] = t[:,[0,2]].astype(int)

array([[  0.        ,   1.4115732 ,   2.        ,   3.5173022 ],
       [  4.        ,   5.35321628,   6.        ,   7.17438118],
       [  8.        ,   9.51710983,  10.        ,  11.00586511]])

otherwise you probably need to split your original array into 2 different arrays, with one containing the column that stay floats, the other containing the column that become ints.

t_int =  t[:,[0,2]].astype(int)

array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])


t_float = t[:,[1,3]]

array([[  1.4115732 ,   3.5173022 ],
       [  5.35321628,   7.17438118],
       [  9.51710983,  11.00586511]])

Note that you'll have to change your indexing accordingly to access your elements...

like image 155
Julien Avatar answered Oct 11 '22 13:10

Julien