0
0
PandasHow-ToBeginner · 3 min read

How to Change Column Data Type in pandas DataFrame

To change a column's data type in pandas, use the astype() method on the DataFrame column, like df['col'] = df['col'].astype(new_dtype). This converts the column to the specified type such as int, float, or str.
📐

Syntax

The basic syntax to change a column's data type is:

  • df['column_name']: Selects the column to convert.
  • astype(new_dtype): Converts the column to the new data type.
  • new_dtype: The target data type, e.g., 'int', 'float', 'str', or 'category'.
python
df['column_name'] = df['column_name'].astype(new_dtype)
💻

Example

This example shows how to convert a column from float to integer and from integer to string.

python
import pandas as pd

data = {'age': [25.0, 30.0, 22.0], 'score': [88, 92, 85]}
df = pd.DataFrame(data)

# Convert 'age' from float to int
df['age'] = df['age'].astype(int)

# Convert 'score' from int to string
df['score'] = df['score'].astype(str)

print(df)
print(df.dtypes)
Output
age score 0 25 88 1 30 92 2 22 85 age int64 score object dtype: object
⚠️

Common Pitfalls

Common mistakes include:

  • Trying to convert non-numeric strings to numbers without cleaning data first, which causes errors.
  • Not assigning the result back to the DataFrame column, so the change is lost.
  • Using astype() on columns with missing values without specifying nullable types.
python
import pandas as pd

data = {'value': ['10', '20', 'thirty']}
df = pd.DataFrame(data)

# Wrong: This will raise an error because 'thirty' is not numeric
# df['value'] = df['value'].astype(int)

# Right: Use pd.to_numeric with errors='coerce' to handle non-numeric
df['value'] = pd.to_numeric(df['value'], errors='coerce')

print(df)
Output
value 0 10.0 1 20.0 2 NaN
📊

Quick Reference

MethodDescriptionExample
astype()Convert column to specified dtypedf['col'] = df['col'].astype('int')
pd.to_numeric()Convert to numeric, handle errorsdf['col'] = pd.to_numeric(df['col'], errors='coerce')
astype('category')Convert column to categorical typedf['col'] = df['col'].astype('category')

Key Takeaways

Use df['col'] = df['col'].astype(new_dtype) to change a column's data type.
Always assign the result back to the DataFrame column to keep changes.
Use pd.to_numeric() with errors='coerce' to safely convert non-numeric strings to numbers.
Be careful converting columns with missing values; consider nullable types.
Common target types include int, float, str, and category.