Pandas Change Datatype Last Updated : 13 Jan, 2025 Summarize Comments Improve Suggest changes Share 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) OutputName object Age float64 Gender object Salary int64 dtype: object Converting a Column to a DateTime TypeSometimes, 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) OutputName object Age int64 Gender object Salary int64 Join Date datetime64[ns] dtype: object Changing Multiple Columns' Data TypesIf 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) OutputName object Age float64 Gender object Salary object dtype: object Comment More infoAdvertise with us Next Article Pandas Change Datatype A abhirajksingh Follow Improve Article Tags : Pandas AI-ML-DS Python-pandas Python pandas-basics Python pandas-io +1 More Similar Reads Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Machin 5 min read Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea 15+ min read Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or 9 min read Logistic Regression in Machine Learning Logistic Regression is a supervised machine learning algorithm used for classification problems. Unlike linear regression which predicts continuous values it predicts the probability that an input belongs to a specific class. It is used for binary classification where the output can be one of two po 11 min read 100+ Machine Learning Projects with Source Code [2025] This article provides over 100 Machine Learning projects and ideas to provide hands-on experience for both beginners and professionals. Whether you're a student enhancing your resume or a professional advancing your career these projects offer practical insights into the world of Machine Learning an 5 min read K means Clustering â Introduction K-Means Clustering is an Unsupervised Machine Learning algorithm which groups unlabeled dataset into different clusters. It is used to organize data into groups based on their similarity. Understanding K-means ClusteringFor example online store uses K-Means to group customers based on purchase frequ 4 min read K-Nearest Neighbor(KNN) Algorithm K-Nearest Neighbors (KNN) is a supervised machine learning algorithm generally used for classification but can also be used for regression tasks. It works by finding the "k" closest data points (neighbors) to a given input and makesa predictions based on the majority class (for classification) or th 8 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read Introduction to Convolution Neural Network Convolutional Neural Network (CNN) is an advanced version of artificial neural networks (ANNs), primarily designed to extract features from grid-like matrix datasets. This is particularly useful for visual datasets such as images or videos, where data patterns play a crucial role. CNNs are widely us 8 min read Naive Bayes Classifiers Naive Bayes is a classification algorithm that uses probability to predict which category a data point belongs to, assuming that all features are unrelated. This article will give you an overview as well as more advanced use and implementation of Naive Bayes in machine learning. Illustration behind 7 min read Like