Pandas: ValueError: cannot convert float NaN to integer
For identifying NaN
values use boolean indexing
:
print(df[df['x'].isnull()])
Then for removing all non-numeric values use to_numeric
with parameter errors='coerce'
- to replace non-numeric values to NaN
s:
df['x'] = pd.to_numeric(df['x'], errors='coerce')
And for remove all rows with NaN
s in column x
use dropna
:
df = df.dropna(subset=['x'])
Last convert values to int
s:
df['x'] = df['x'].astype(int)
Numpy: ValueError: cannot convert float NaN to integer (Python)
Designate a type for your array of float32
(or float16
, float64
, etc. as appropriate)
import numpy as np
A = np.array([10, 20, 30, 40, 50, 60, 70], dtype=np.float32)
C=[2,4]
A=np.insert(A,C,np.NaN,axis=0)
print("A =",[A])
A = [array([10., 20., nan, 30., 40., nan, 50., 60., 70.], dtype=float32)]
How can I solve the issue 'ValueError: cannot convert float NaN to integer' in python
You need to say what you want to do with nans. You can either drop those rows (df.dropna()
) or replace nans with something else (0
for instance: df.fillna(0)
)
df["Height (cm)"]= df["Height (cm)"].fillna(0).astype(int)
ValueError: cannot convert float NaN to integer
That is because nan
is recognised as a special character for float arrays (a sort of special float
), and apparently your x_2
array is int
type; and nan
cannot be converted to int
to fit into your array.
A workaround could be casting your array into floats:
>>>import numpy as np
>>>a = np.array([[1,2,3], [4,5,6]])
>>>a
array([[1, 2, 3],
[4, 5, 6]])
>>>a[0,0] = np.nan
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-43-8e6dceaece5a> in <module>
----> 1 a[0,0] = np.nan
ValueError: cannot convert float NaN to integer
>>>a = a.astype('float32')
>>>a[0,0] = np.nan
>>>a
array([[nan, 2., 3.],
[ 4., 5., 6.]], dtype=float32)
How to resolve Pandas: ValueError: cannot convert float NaN to integer for a large dataset?
Your commands work for me. Can you try updating your numpy and pandas ?
Related Topics
How to Add a Custom Loglevel to Python's Logging Facility
Creating a Class Within a Function and Access a Function Defined in the Containing Function's Scope
Lambda Function Don't Closure the Parameter in Python
Generalise Slicing Operation in a Numpy Array
How to Write Strategy Pattern in Python Differently Than Example in Wikipedia
How to Share Numpy Random State of a Parent Process with Child Processes
Nltk Named Entity Recognition to a Python List
How to Understand the Output of Dis.Dis
Keep Same Dummy Variable in Training and Testing Data
Numpy to Tfrecords: Is There a More Simple Way to Handle Batch Inputs from Tfrecords
How to Append to the End of an Empty List
Pip Install Gives Error: Unable to Find Vcvarsall.Bat
How to Create an Object and Add Attributes to It
Add Column with Constant Value to Pandas Dataframe
How to Get the Executable's Current Directory in Py2Exe
Sampling Uniformly Distributed Random Points Inside a Spherical Volume
What Does "Error: Option --Single-Version-Externally-Managed Not Recognized" Indicate