Pandas Tutorial: How to Change the Data Type of…
In this short, Pandas tutorial, you will learn how to change the data type of columns in the dataframe. First, you will learn how to change the data type of one column. After that, you will learn how to change the data type of two (or many) columns. This task can, in general, be seen as a method for data manipulation in Python.
No matter if you need to change the data type of characters float , or integers, this tutorial will show you how to do it. Previously you have learned how to rename columns in a Pandas dataframe, and append a column to a Pandas dataframe, here you will continue to learn working with Pandas dataframes.
If course, you need to have Pandas installed and if you are unsure you can check the post about how to list all installed Python packages before you continue.
Example Data
In this tutorial, you will learn how to change the data type of columns in a Pandas dataframe. First, however, you need some example data to practice on. Now, here you are creating a Pandas dataframe from a Python dictionary. In the first step, however, you import Pandas as pd.
import pandas as pd
data = {'A':[0, 2, 3, 4, 5, 6],
'B':['0', '3', '4', '5', '7', '99'],
'C':['F', 'D', 'D', 'D', 'F', 'F'],
'D':['0.1', '0.33', '0.44', '0.444',
'0.443', '0.444']}
df = pd.DataFrame(data)
display(df)
If you have a look at what data types we have in this Pandas dataframe:
df.types
In the code chunk above, the dtypes method (of the DataFrame object) was used to display which types we have in the df. In the next section, you will learn how to change the type of one Pandas column.
Example 1: Convert One Variable of a DataFrame to Integer
Now, in the first example you will learn how to change one of the variables from object to Integer. As you remember, the dataframe contains columns with string variables that are actual numbers (i.e., integers and float). To carry out statistical calculations on these numbers you’ll have to convert the values in a column, for instance, to another type.
Here, you will learn how to change type of one dataframe column (i.e., ‘B’) and you can use the pd.to_numeric() method to accomplish this:
Pandas Change Type of a Column to Integer:
Here’s how to change the type of a column to integer:
df['B'] = pd.to_numeric(df['B'])
df.dtypes
To summarize, if you want to change the type of a column you can select the column and use the to_numeric method available. It is, of course, also possible to import numpy as np and use the np.int64:
Example 2: Convert the type of Multiple Variables in a Pandas DataFrame
In the second example, you are going to learn how to change the type of two columns in a Pandas dataframe. In the example, you will use Pandas apply() method as well as the to_numeric to change the two columns containing numbers to numeric values.
df[['B', 'D']] = df[['B', 'D']].apply(pd.to_numeric)
Now, what becomes evident here is that Pandas to_numeric convert the types in the columns to integer and float. I mean, we had one column with integer (‘B’) and one with float values (‘D’) and these are automatically converted to these types.
Method 2: Using Pandas apply()
Another option, is to use the apply method togetehr with pd.to_numeric and add the “error” argument. Now, this is something you can do to change ALL possible columns to numeric type:
df.apply(pd.to_numeric, errors="ignore)
Conclusion: Change Type of Pandas Column
In this post you learned now easy it is to convert type of one column or many columns in a Pandas dataframe. First, you learned how to change one column using the to_numeric method. Second, you learned two methods on how to change many (or all) columns data types to numeric. Hope you learned something and drop a line below if there are any topics you want to learn more about here on Python daddy!