
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Change Data Type for one or more columns in Pandas Dataframe
Many times we may need to convert the data types of one or more columns in a pandas data frame to accommodate certain needs of calculations. There are some in-built functions or methods available in pandas which can achieve this.
Using astype()
The astype() method we can impose a new data type to an existing column or all columns of a pandas data frame. In the below example we convert all the existing columns to string data type.
Example
import pandas as pd #Sample dataframe df = pd.DataFrame({ 'DayNo': [1, 2, 3, 4, 5,6,7], 'Name': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu','Fri','Sat'], 'Qty': [2.6, 5, 11.8, 2, 5.6,0,0.25]}) # Exisitng Data types print(df.dtypes) #Convert to string data type df_str = df.astype(str) # Verify the conversion print("***After Conversion***") print(df_str.dtypes)
Output
Running the above code gives us the following result −
DayNo int64 Name object Qty float64 dtype: object ***After Conversion*** DayNo object Name object Qty object dtype: object
Using to_numeric()
We can convert the numbers which are currently marked as string in the data frame to numeric using to_numeric().
Example
import pandas as pd # Example dataframe df = pd.DataFrame({ 'DayNo': [1, 2, 3, 4, 5,6,7], 'Name': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu','Fri','Sat'], 'Qty': [2.6, 5, 11.8, 2, 5.6,0,0.25]}) df_str = df.astype(str) print(df_str.dtypes) #Applying conversion print("After Conversion:") df_num = pd.to_numeric(df_str.DayNo) print('DayNo:',df_num.dtypes)
Running the above code gives us the following result −
Output
DayNo object Name object Qty object dtype: object After Conversion: DayNo: int64
Using infer_objects()
It is a method of soft conversion where we convert columns of a DataFrame that have an object datatype to a more specific type.
Example
import pandas as pd # Example dataframe df = pd.DataFrame({ 'DayNo': [1, 2, 3, 4, 5,6,7], # 'Name': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu','Fri','Sat'], 'Qty': ['2.6', '5', '11.8', '2', '5.6','0','0.25']}, dtype='object') print(df.dtypes) #Applying conversion print("After Conversion:") df_new = df.infer_objects() print(df_new.dtypes)
Running the above code gives us the following result −
Output
DayNo object Qty object dtype: object After Conversion: DayNo int64 Qty object dtype: object
- Related Articles
- Change Data Type for one or more columns in Pandas Dataframe
- Python Pandas - Plot multiple data columns in a DataFrame?
- How to change the order of Pandas DataFrame columns?
- How to check the data type in pandas DataFrame?
- Write a program in Python to remove one or more than one columns in a given DataFrame
- How to select all columns except one in a Pandas DataFrame?
- Python - Grouping columns in Pandas Dataframe
- Drop One or Multiple Columns From PySpark DataFrame
- Drop rows from Pandas dataframe with missing values or NaN in columns
- Select multiple columns in a Pandas DataFrame
- How to get list of all columns except one or more columns from an R data frame?
- Python - Name columns explicitly in a Pandas DataFrame
- How to create a data frame with one or more columns as a list in R?
- Python - Renaming the columns of Pandas DataFrame
- What should one use CHAR data type or VARCHAR data type in MySQL?
