Change column type in pandas
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
You must login to ask question.
You have four main options for converting types in pandas:
to_numeric()
– provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type. (See alsoto_datetime()
andto_timedelta()
.)astype()
– 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).infer_objects()
– a utility method to convert object columns holding Python objects to a pandas type if possible.convert_dtypes()
– convert DataFrame columns to the “best possible” dtype that supportspd.NA
(pandas’ object to indicate a missing value).