Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How can I convert one string column to float considering that it contains N/A values? I've tried to use the to_numeric function, but I'm receiving an error: "Unable to parse string "N/A" at position.
np.where(df["your column"].isna() is False, df["your column"], df["your column"].astype(float))
df["your column"].replace("N/A", np.nan).astype(float)
The solution that worked in my case was to add an argument errors='coerce' to the to_numeric function.
df["mycolumn"] = pd.to_numeric(df["mycolumn"], errors='coerce', downcast="float")
Add a comment
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
np.where(df["your column"].isna() is False, df["your column"], df["your column"].astype(float))df["your column"].replace("N/A", np.nan).astype(float)