1df_raw['PricePerSeat_Outdoor'] = pd.to_numeric(df_raw['PricePerSeat_Outdoor'], errors='coerce')
1>>> s = pd.Series(["8", 6, "7.5", 3, "0.9"]) # mixed string and numeric values
2>>> s
30 8
41 6
52 7.5
63 3
74 0.9
8dtype: object
9
10>>> pd.to_numeric(s) # convert everything to float values
110 8.0
121 6.0
132 7.5
143 3.0
154 0.9
16dtype: float64
17
1You have four main options for converting types in pandas:
2
3to_numeric() - provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type. (See also to_datetime() and to_timedelta().)
4
5astype() - convert (almost) any type to (almost) any other type (even if it's not necessarily sensible to do so). Also allows you to convert to categorial types (very useful).
6
7infer_objects() - a utility method to convert object columns holding Python objects to a pandas type if possible.
8
9convert_dtypes() - convert DataFrame columns to the "best possible" dtype that supports pd.NA (pandas' object to indicate a missing value).
10
11Read on for more detailed explanations and usage of each of these methods.