
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change Data Type for one or more columns in Pandas Dataframe
In this tutorial, we are going to learn about the conversion of one or more columns data type into another data type. We are going to use the method DataFrame.astype() method.
We have to pass any data type from Python, Pandas, or Numpy to change the column elements data types. We can also give a dictionary of selected columns to change particular column elements data types. Let's see the examples with code.
Example
# importing the pandas library import pandas as pd # creating a DataFrame data_frame = pd.DataFrame({'No': [1, 2, 3], 'Name': ['Tutorialspoint', 'Mohit', 'Sharma'], 'Age': [25, 32, 21]}) # we will change the data type of all columns to str data_frame = data_frame.astype(str) # checking the data types using data_frame.dtypes method print(data_frame.dtypes)
Output
All the column data types changed to str objects. If you run the above program, you will get the following results.
No object Name object Age object dtype: object
Now, let's try to change the data type of Age column from int to str. We have to create a dictionary specifying column name and desired data type.
Example
# importing the pandas library import pandas as pd # creating a DataFrame data_frame = pd.DataFrame({'No': [1, 2, 3], 'Name': ['Tutorialspoint', 'Mohit', 'Sharma'], 'Age': [25, 32, 21]}) # creating a dictionary with column name and data type data_types_dict = {'Age': str} # we will change the data type of Age column to str by giving the dict to the astype method data_frame = data_frame.astype(data_types_dict) # checking the data types using data_frame.dtypes method print(data_frame.dtypes)
Output
If you see the output, only the Age column data type is changed from int to str. See the result below.
No int64 Name object Age object dtype: object
Conclusion
If you face any difficulties in following the tutorial, mention them in the comment section.
- Related Questions & Answers
- Write a program in Python to remove one or more than one columns in a given DataFrame
- How to change the order of Pandas DataFrame columns?
- How to check the data type in pandas DataFrame?
- Python Pandas - Plot multiple data columns in a DataFrame?
- How to get list of all columns except one or more columns from an R data frame?
- How to select all columns except one in a Pandas DataFrame?
- What should one use CHAR data type or VARCHAR data type in MySQL?
- Python - Grouping columns in Pandas Dataframe
- How to create a data frame with one or more columns as a list in R?
- Select multiple columns in a Pandas DataFrame
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n
- Python - Renaming the columns of Pandas DataFrame
- How to find rows with exact value in one or more columns with MySQL?
- Python - Name columns explicitly in a Pandas DataFrame