Python type conversion from int to float

I am trying to change data_df, which is a float64 type for int.

data_df['grade'] = data_df['grade'].astype(int) 

I get the following error.

invalid literal for int () with base 10: '17 .44 '

+6
source share
3 answers

It seems to me that you need to_numeric , because float cannot be attributed to int :

 data_df['grade'] = pd.to_numeric(data_df['grade']).astype(int) 

Another solution is first added to the float , and then to int :

 data_df['grade'] = data_df['grade'].astype(float).astype(int) 

Example:

 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 an error:

ValueError: cannot parse string

you can add the parameter errors='coerce' to convert non-numeric to NaN .

If the values ​​are NaN , then it is impossible to distinguish from int , 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 you want to change NaN to some numeric ones, for example. 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 

A little tip:

Before using errors='coerce' , check all strings where it is not possible to list on numeric characters boolean indexing :

 print (data_df[pd.to_numeric(data_df['grade'], errors='coerce').isnull()]) grade 3 aa 
+8
source

that works data_df['grade'] = int(pd.to_numeric(data_df['grade'])) The as_type(int) data_df['grade'] = int(pd.to_numeric(data_df['grade'])) method causes errors and errors because it wants to tell you that exact conversion from float to integer is impossible, and you will lose information. My solution truncates an integer (i.e. 1.9 becomes 1), so you can indicate in your question that you want to convert float to integer by truncating or rounding (i.e. 1.9 becomes 2)

+3
source

I found this to work for me, where none of the other earlier answers helped me:

data_df['grade'] = data_df['grade'].apply(np.int)

0
source

Source: https://habr.com/ru/post/1012507/


All Articles