Change Data Types
astype converts a column. to_numeric parses messy text and errors="coerce" turns bad values into NaN.
Example
import pandas as pd
import numpy as np
s = pd.Series(['1', '2', 'x', '4'])
print(pd.to_numeric(s, errors='coerce').tolist())
print(pd.Series([1, 2, 3]).astype(float).tolist())
print(pd.Series([0, 1, 1]).astype(bool).tolist())Result
[1.0, 2.0, nan, 4.0] [1.0, 2.0, 3.0] [False, True, True]
Exercise
Convert to float.
s.("float64")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.