Issue
Following is the code part that the above issue appeared.
df["Height (cm)"]= df["Height (cm)"].astype(int)
The Dtype of "Height (cm)" was initially 'object' and the missing values of it were replaced with np.nan. Then I tried to execute the above code to convert Height into an integer type, but ended up by having the error 'ValueError: cannot convert float NaN to integer'. How can I solve this issue?
Solution
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)
Answered By - ted
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.