Change column type in pandas

In pandas, you can change the data type of a column using the astype() function. You can pass a dictionary to the astype() function where the keys are the column names and the values are the new data types.

For example, if you have a DataFrame called df and you want to change the data type of the column "A" to float, you would do the following:

df["A"] = df["A"].astype(float)

Watch a course Python - The Practical Guide

You can also change the data type of multiple columns at once by passing a dictionary to the astype() function. For example:

df = df.astype({"A": float, "B": int, "C": str})

If you want to change the data type of a particular column and also want to change the values of that column in case of error during conversion, you can use the pd.to_numeric and errors='coerce'

df['A'] = pd.to_numeric(df['A'], errors='coerce')

You can also use the pd.to_datetime to change datetime type of column.

df['date'] = pd.to_datetime(df['date'])

Please make sure the operation suits your needs and won't cause any unexpected values or data loss.