numpy.ndarray.tolist will do it:
a.tolist()
If your data is a pandas series you can call their tolist wrapper with the same result.
Answer from bigonazzi on Stack Overflowpython - Convert 2D float array to 2D int array in NumPy - Stack Overflow
python - Converting numpy array values into integers - Stack Overflow
python - convert numpy array to integer array - Stack Overflow
python - Convert Numpy array with 'n' and 'y' into integer array of 0 and 1 - Data Science Stack Exchange
Videos
Use the astype method.
>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> x.astype(int)
array([[1, 2],
[1, 2]])
Some numpy functions for how to control the rounding: rint, floor,trunc, ceil. depending how u wish to round the floats, up, down, or to the nearest int.
>>> x = np.array([[1.0,2.3],[1.3,2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> y = np.trunc(x)
>>> y
array([[ 1., 2.],
[ 1., 2.]])
>>> z = np.ceil(x)
>>> z
array([[ 1., 3.],
[ 2., 3.]])
>>> t = np.floor(x)
>>> t
array([[ 1., 2.],
[ 1., 2.]])
>>> a = np.rint(x)
>>> a
array([[ 1., 2.],
[ 1., 3.]])
To make one of this in to int, or one of the other types in numpy, astype (as answered by BrenBern):
a.astype(int)
array([[1, 2],
[1, 3]])
>>> y.astype(int)
array([[1, 2],
[1, 2]])
Make a sample array
In [206]: x=np.array([1e9, 2e10, 1e6])
In [207]: x
Out[207]: array([ 1.00000000e+09, 2.00000000e+10, 1.00000000e+06])
We can convert to ints - except notice that the largest one is too large the default int32
In [208]: x.astype(int)
Out[208]: array([ 1000000000, -2147483648, 1000000])
In [212]: x.astype(np.int64)
Out[212]: array([ 1000000000, 20000000000, 1000000], dtype=int64)
Writing a csv with the default format (float) (this is the default format regardless of the array dtype):
In [213]: np.savetxt('text.txt',x)
In [214]: cat text.txt
1.000000000000000000e+09
2.000000000000000000e+10
1.000000000000000000e+06
We can specify a format:
In [215]: np.savetxt('text.txt',x, fmt='%d')
In [216]: cat text.txt
1000000000
20000000000
1000000
Potentially there are 3 issues:
- integer v float in the array itself, it's
dtype - display or print of the array
- writing the array to a csv file
It is a printing option, see the documentation: printing options. Briefly stated: you need to use the suppress option when printing:
np.set_printoptions(suppress=True) # for small floating point.
np.set_printoptions(suppress=True, formatter={'all':lambda x: str(x)})
It is quite trivial
(X=='y').astype(int)
Should do the trick. It simply converts your array to True or False according to your requirements and then astype will impose the required datatype. By default int will give you 1 for True and 0 for False.
You could use the following code:
X[X=='y'] = 1
X[X=='n'] = 0
This replaces the indexes of 'y' with 1 and of 'n' with 0.
Generally the X=='y' returns a Boolean array which contains True where the 'y' and False everywhere else and so on.
import numpy as np
a = np.array([['-0.99', '', '0.56', '0.56', '-2.02', '-0.96']])
a[a == ''] = 0.0
a = a.astype(np.float)
Result is:
[[-0.99 0. 0.56 0.56 -2.02 -0.96]]
Your values are floats, not integers. It is not clear if you want a list of lists or a numpy array as your end result. You can easily get a list of lists like this:
a = a.tolist()
Result:
[[-0.99, 0.0, 0.56, 0.56, -2.02, -0.96]]
That is a pure python solution and it produces a list .
With simple python operations, you can map inner list with float. That will convert all string elements to float and assign it as the zero indexed item of your list.
a = [['-0.99' , '0.56' , '0.56' , '0.56', '-2.02' , '-0.96']]
a[0] = map(float, a[0])
print a
[[-0.99, 0.56, 0.56, 0.56, -2.02, -0.96]]
Update: Try the following
a = [['-0.99' , '0.56' , '0.56' , '0.56', '-2.02' , '-0.96', '', 'nan']]
for _position, _value in enumerate(a[0]):
try:
_new_value = float(_value)
except ValueError:
_new_value = 0.0
a[0][_position] = _new_value
[[-0.99, 0.56, 0.56, 0.56, -2.02, -0.96, 0.0, nan]]
It enumerates the objects in the list and try to parse them to float, if it fails, then replace it with 0.0
Use .astype.
>>> a = numpy.array([1, 2, 3, 4], dtype=numpy.float64)
>>> a
array([ 1., 2., 3., 4.])
>>> a.astype(numpy.int64)
array([1, 2, 3, 4])
See the documentation for more options.
While astype is probably the "best" option there are several other ways to convert it to an integer array. I'm using this arr in the following examples:
>>> import numpy as np
>>> arr = np.array([1,2,3,4], dtype=float)
>>> arr
array([ 1., 2., 3., 4.])
The int* functions from NumPy
>>> np.int64(arr)
array([1, 2, 3, 4])
>>> np.int_(arr)
array([1, 2, 3, 4])
The NumPy *array functions themselves:
>>> np.array(arr, dtype=int)
array([1, 2, 3, 4])
>>> np.asarray(arr, dtype=int)
array([1, 2, 3, 4])
>>> np.asanyarray(arr, dtype=int)
array([1, 2, 3, 4])
The astype method (that was already mentioned but for completeness sake):
>>> arr.astype(int)
array([1, 2, 3, 4])
Note that passing int as dtype to astype or array will default to a default integer type that depends on your platform. For example on Windows it will be int32, on 64bit Linux with 64bit Python it's int64. If you need a specific integer type and want to avoid the platform "ambiguity" you should use the corresponding NumPy types like np.int32 or np.int64.