0
$\begingroup$

I have data with missing values. All columns are integer, except for a column that has missing values. These missing values, were set with a "?" which was converted to NaN using the Numpy function. This is the result of using df.info():

enter image description here

So only one column is an Object type, which makes sense, because that is where the NaN is. When I try to use the KNN-imputer, it gives error :"ValueError: could not convert string to float: )

I got around this by converting the Pandas Dataframe to Numpy Array. But how to I get around this if I just want to use the Pandas Array.

Hope someone can help.

$\endgroup$
1
  • $\begingroup$ Could you provide the full code that generate the error , including line where you did replace "?" with NaN $\endgroup$ Dec 5, 2023 at 21:19

1 Answer 1

2
$\begingroup$

After replacing the ? with np.NaN, you will be able to convert the type of the BareNuc column from object to float (as np.NaN is considered a special floating-point value and cannot be converted to any other type than float). with that, you wouldn’t need to convert to a NumPy array.

df = df.astype({"BareNuc": float})

At the end, all columns will be used as float type, so that wouldn’t affect the resource allocation.

$\endgroup$
1
  • $\begingroup$ Hi El Houcine, thanks for your reply. It looks very good. I will give you this question! Thanks so much! $\endgroup$
    – Palu
    Dec 6, 2023 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.