Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Pandas Change Datatype
Next article icon

Pandas Change Datatype

Last Updated : 13 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In data analysis, ensuring that each column in a Pandas DataFrame has the correct data type is crucial for accurate computations and analyses. The most common way to change the data type of a column in a Pandas DataFrame is by using the astype() method. This method allows you to convert a specific column to a desired data type. Here's the example:

Using astype() method

Python
import pandas as pd  data = {'Name': ['John', 'Alice', 'Bob', 'Eve', 'Charlie'],          'Age': [25, 30, 22, 35, 28],          'Gender': ['Male', 'Female', 'Male', 'Female', 'Male'],          'Salary': [50000, 55000, 40000, 70000, 48000]}  df = pd.DataFrame(data)  # Convert 'Age' column to float type df['Age'] = df['Age'].astype(float) print(df.dtypes) 

Output
Name       object Age       float64 Gender     object Salary      int64 dtype: object 

Converting a Column to a DateTime Type

Sometimes, a column that contains date information may be stored as a string. You can convert it to the datetime type using the pd.to_datetime() function.

Python
# Example: Create a 'Join Date' column as a string df['Join Date'] = ['2021-01-01', '2020-05-22', '2022-03-15', '2021-07-30', '2020-11-11']  # Convert 'Join Date' to datetime type df['Join Date'] = pd.to_datetime(df['Join Date']) print(df.dtypes) 

Output
Name                 object Age                   int64 Gender               object Salary                int64 Join Date    datetime64[ns] dtype: object 

Changing Multiple Columns' Data Types

If you need to change the data types of multiple columns at once, you can pass a dictionary to the astype() method, where keys are column names and values are the desired data types.

Python
# Convert 'Age' to float and 'Salary' to string df = df.astype({'Age': 'float64', 'Salary': 'str'}) print(df.dtypes) 

Output
Name       object Age       float64 Gender     object Salary     object dtype: object 

Next Article
Pandas Change Datatype

A

abhirajksingh
Improve
Article Tags :
  • Pandas
  • AI-ML-DS
  • Python-pandas
  • Python pandas-basics
  • Python pandas-io

Similar Reads

    Change String To Date In Pandas Dataframe
    Working with date and time data in a Pandas DataFrame is common, but sometimes dates are stored as strings and need to be converted into proper date formats for analysis and visualization. In this article, we will explore multiple methods to convert string data to date format in a Pandas DataFrame.U
    5 min read
    Create empty dataframe in Pandas
    The Pandas Dataframe is a structure that has data in the 2D format and labels with it. DataFrames are widely used in data science, machine learning, and other such places. DataFrames are the same as SQL tables or Excel sheets but these are faster in use.Empty DataFrame could be created with the help
    1 min read
    Pandas Drop Column
    When working with large datasets, there are often columns that are irrelevant or redundant. Pandas provides an efficient way to remove these unnecessary columns using the `drop()` function. In this article, we will cover various methods to drop columns from a DataFrame.Pythonimport pandas as pd data
    4 min read
    Convert JSON to Pandas DataFrame
    When working with data, it's common to encounter JSON (JavaScript Object Notation) files, which are widely used for storing and exchanging data. Pandas, a powerful data manipulation library in Python, provides a convenient way to convert JSON data into a Pandas data frame. In this article, we'll exp
    4 min read
    Python | Pandas DataFrame.astype()
    DataFrame.astype() function in pandas cast a pandas object such as a DataFrame or Series to a specified data type. This is especially useful when you need to ensure that columns have the correct type, such as converting strings to integers or floats to strings. For example:Pythonimport pandas as pd
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences