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.
Answer from kennytm on Stack OverflowUse .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.
Solution for pandas 0.24+ for converting numeric with missing values:
df = pd.DataFrame({'column name':[7500000.0,7500000.0, np.nan]})
print (df['column name'])
0 7500000.0
1 7500000.0
2 NaN
Name: column name, dtype: float64
df['column name'] = df['column name'].astype(np.int64)
ValueError: Cannot convert non-finite values (NA or inf) to integer
#http://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html
df['column name'] = df['column name'].astype('Int64')
print (df['column name'])
0 7500000
1 7500000
2 NaN
Name: column name, dtype: Int64
I think you need cast to numpy.int64:
df['column name'].astype(np.int64)
Sample:
df = pd.DataFrame({'column name':[7500000.0,7500000.0]})
print (df['column name'])
0 7500000.0
1 7500000.0
Name: column name, dtype: float64
df['column name'] = df['column name'].astype(np.int64)
#same as
#df['column name'] = df['column name'].astype(pd.np.int64)
print (df['column name'])
0 7500000
1 7500000
Name: column name, dtype: int64
If some NaNs in columns need replace them to some int (e.g. 0) by fillna, because type of NaN is float:
df = pd.DataFrame({'column name':[7500000.0,np.nan]})
df['column name'] = df['column name'].fillna(0).astype(np.int64)
print (df['column name'])
0 7500000
1 0
Name: column name, dtype: int64
Also check documentation - missing data casting rules
EDIT:
Convert values with NaNs is buggy:
df = pd.DataFrame({'column name':[7500000.0,np.nan]})
df['column name'] = df['column name'].values.astype(np.int64)
print (df['column name'])
0 7500000
1 -9223372036854775808
Name: column name, dtype: int64
You can need to pass in the string 'int64':
>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1.0, 2.0]}) # some test dataframe
>>> df['a'].astype('int64')
0 1
1 2
Name: a, dtype: int64
There are some alternative ways to specify 64-bit integers:
>>> df['a'].astype('i8') # integer with 8 bytes (64 bit)
0 1
1 2
Name: a, dtype: int64
>>> import numpy as np
>>> df['a'].astype(np.int64) # native numpy 64 bit integer
0 1
1 2
Name: a, dtype: int64
Or use np.int64 directly on your column (but it returns a numpy.array):
>>> np.int64(df['a'])
array([1, 2], dtype=int64)
python - convert number from numpy.float64 to integer - Stack Overflow
problems when converting data from float64 to int64 in python for cross validation
python 3.x - Converting float64 to integer - Stack Overflow
Int too large
try this:
df = pd.DataFrame({'sales': {0: 4.000, 1: 6.454, 2: float('nan'), 3:23.463, 4:5.654}})
df = (df.sales.fillna(0)*1000).astype(int)
#OR
df = (df.sales.fillna(0)*1000).astype(np.int64)
print(df)
output:
0 4000
1 6454
2 0
3 23463
4 5654
Name: sales, dtype: int64
EDIT: if you want converting currency with $ to int64 you can try this:
df = pd.DataFrame({'sales': {0: '$4.000', 1: '$6.454', 2: float('nan'), 3: '$23.463', 4:'$5.654'}})
print(df)
df['sales'] = (((df['sales'].replace('[\$,]', '', regex=True).astype(float)).fillna(0))*1000).astype(np.int64)
print();print(df)
output:
sales
0 $4.000
1 $6.454
2 NaN
3 $23.463
4 $5.654
sales
0 4000
1 6454
2 0
3 23463
4 5654
Based on your comments and if I understand correctly, you want:
- to fill empty values with
0. - to multiply the entire series by 1000.
- to cast the result as
int64.
Did I understand you correctly?
(df.sales.fillna(0) * 1000).astype(int64)