Reputation: 129
I am trying to change data_df
which is type float64
to int
.
data_df['grade'] = data_df['grade'].astype(int)
I get the following error.
invalid literal for int() with base 10: '17.44'
Upvotes: 9
Views: 14625
Reputation: 338
From:
data_df['grade'] = data_df['grade'].astype(int)
Need to change int into 'int'
data_df['grade'] = data_df['grade'].astype('int')
Upvotes: 2
Reputation: 41
I found this to work for me where none of the other earlier answers did the job for me:
data_df['grade'] = data_df['grade'].apply(np.int)
Upvotes: 1
Reputation: 863741
I think you need to_numeric
first because float
cannot be cast to int
:
data_df['grade'] = pd.to_numeric(data_df['grade']).astype(int)
Another solution is first cast to float
and then to int
:
data_df['grade'] = data_df['grade'].astype(float).astype(int)
Sample:
data_df = pd.DataFrame({'grade':['10','20','17.44']})
print (data_df)
grade
0 10
1 20
2 17.44
data_df['grade'] = pd.to_numeric(data_df['grade']).astype(int)
print (data_df)
grade
0 10
1 20
2 17
data_df['grade'] = data_df['grade'].astype(float).astype(int)
print (data_df)
grade
0 10
1 20
2 17
If some values cannot be converted and after to_numeric
get error:
ValueError: Unable to parse string
is possible add parameter errors='coerce'
for convert non numeric to NaN
.
If NaN
values then cast to int
is not possible see docs:
data_df = pd.DataFrame({'grade':['10','20','17.44', 'aa']})
print (data_df)
grade
0 10
1 20
2 17.44
3 aa
data_df['grade'] = pd.to_numeric(data_df['grade'], errors='coerce')
print (data_df)
grade
0 10.00
1 20.00
2 17.44
3 NaN
If want change NaN
to some numeric e.g. 0
use fillna
:
data_df['grade'] = pd.to_numeric(data_df['grade'], errors='coerce')
.fillna(0)
.astype(int)
print (data_df)
grade
0 10
1 20
2 17
3 0
Small advice:
Before using errors='coerce'
check all rows where is impossible casting to numeric by boolean indexing
:
print (data_df[pd.to_numeric(data_df['grade'], errors='coerce').isnull()])
grade
3 aa
Upvotes: 11
Reputation: 971
what works is data_df['grade'] = int(pd.to_numeric(data_df['grade']))
The method as_type(int)
throws and error because it want's to tell you, that no exact conversion from float to integer is possible and you will lose information.
My solution will truncate the integer (i.e. 1.9 will become 1), so you might want to specifiy in your question wether you want to convert float to integer by truncation or by rounding (i.e. 1.9 will become 2)
Upvotes: 3