Pandas Data Types Casting

Last Updated: 10 Dec 2025


Pandas allow to cast data types of a DataFrame or Series using the astype() method.Some of the most common data types are:

  • int16, int32, int64, uint16, uint32, uint64
  • float16, float32, float64
  • bool
  • object
  • category
  • datetime64

Integer Data Types

df = pd.DataFrame({
    'Age': ['25', 30, '22', 28],
    'Score': [80, 90, 75, 56],
    'Price': [10.5, 20.75, 15.0, 30.25],
    'Tax': [0.5, 1.0, 0.75, 1.5]
})
df['Age'] = df['Age'].astype(int)
  • Converts the 'Age' column to integer data type.
  • It will convert '25' to 25 and '30' to 30. So you can apply mathematical operations on 'Age' column. like min,max,sum,round etc

String Data Types

df = pd.DataFrame({
    'Name': ['Aman Kumar', 'Nisha Singh', 'Sam Singh'],
    'City': ['Uttar Pradesh', 'Delhi', 'Panjab']
})

Some common operations on string data types are:

df['Name_upper'] = df['Name'].str.upper()
df['City_lower'] = df['City'].str.lower()

df['First_name'] = df['Name'].str.split().str[0]
df['Last_name'] = df['Name'].str.split().str[-1]

df['City_clean'] = df['City'].str.replace('Uttar Pradesh', 'UP')

df['Name_length'] = df['Name'].str.len()

Categorical Data Types

df = pd.DataFrame({
    'Color': ['Red', 'Blue', 'Green', 'Red', 'Blue'],
    'Size': ['Small', 'Medium', 'Large', 'Small', 'Large']
})
df
df['Color'] = df['Color'].astype('category')
df['Size'] = df['Size'].astype('category')

Some common operations on categorical data types are:

  • .cat.categories to get the categories
  • .cat.codes to get the numeric codes for analysis
  • .cat.reorder_categories() to reorder categories
  • .cat.add_categories() to add new categories
  • .cat.remove_categories() to remove categories
  • .cat.rename_categories() to rename categories

print(df['Color'].cat.categories)
print(df['Size'].cat.categories)

df['Color'] = df['Color'].cat.add_categories(['Yellow'])

df['Color'] = df['Color'].cat.rename_categories({'Red': 'R', 'Blue': 'B', 'Green': 'G', 'Yellow': 'Y'})

size_order = ['Small', 'Medium', 'Large']

df['Size'] = pd.Categorical(df['Size'], categories=size_order, ordered=True)

print(df['Size'])
print(df['Size'].min())
print(df['Size'].max())