ValueError: Cannot convert non-finite values (NA or inf) to integer in Python
Hello guys. I just beginning learn Python 2 weeks. I am studying analysis data by using pandas
package. I have created a program as below:
import pandas as pd
import numpy as np
#create DataFrame
df = pd.DataFrame({'points': [22, 11, 13, 14, 17, 26, 29, 30],
'assists': [1, 9, 9, 6, 18, 8, 8, 6],
'rebounds': [12, np.nan, 12, 6, 4, np.nan, 8, 17]})
df['rebounds'] = df['rebounds'].astype(int)
print(df)
When running the code above I got an error ValueError: Cannot convert non-finite values (NA or inf) to integer.
Traceback (most recent call last):
File "HelloWorld.py", line 9, in <module>
df['rebounds'] = df['rebounds'].astype(int)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 5548, in astype
new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors,)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 604, in astype
return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 409, in apply
applied = getattr(b, f)(**kwargs)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py", line 595, in astype
values = astype_nansafe(vals1d, dtype, copy=True)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/dtypes/cast.py", line 968, in astype_nansafe
raise ValueError("Cannot convert non-finite values (NA or inf) to integer")
ValueError: Cannot convert non-finite values (NA or inf) to integer
I'm using Python 3.9.5 and Windows 11.
How can I fix it? Thanks for any suggestions.
-
E0
Erpcrew Agra Mar 19 2022
This error is because your array has Nan values, it can not convert to integer.
Solution: you can use
dropna()
method to help drop NAN or NULL values. See example:import pandas as pd import numpy as np #create DataFrame df = pd.DataFrame({'points': [22, 11, 13, 14, 17, 26, 29, 30], 'assists': [1, 9, 9, 6, 18, 8, 8, 6], 'rebounds': [12, np.nan, 12, 6, 4, np.nan, 8, 17]}) df = df.dropna() df['rebounds'] = df['rebounds'].astype(int) print(df)
#Output
points assists rebounds 0 22 1 12 2 13 9 12 3 14 6 6 4 17 18 4 6 29 8 8 7 30 6 17
I hope it helpful for you.
-
J0
Jesus Lopez Mar 19 2022
Your array contains Nan or Null values. So it can not convert to int.
I usually use
fillna()
method to help convert null /nan to 0 numbers. You can see the example below:import pandas as pd import numpy as np #create DataFrame df = pd.DataFrame({'points': [22, 11, 13, 14, 17, 26, 29, 30], 'assists': [1, 9, 9, 6, 18, 8, 8, 6], 'rebounds': [12, np.nan, 12, 6, 4, np.nan, 8, 17]}) df['rebounds'] = df['rebounds'].fillna(0) # Convert nan/null to 0 df['rebounds'] = df['rebounds'].astype(int) print(df)
#Output
points assists rebounds 0 22 1 12 1 11 9 0 2 13 9 12 3 14 6 6 4 17 18 4 5 26 8 0 6 29 8 8 7 30 6 17
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.