Updated April 1, 2023
Definition of Pandas Converts Column to Int
Pandas is a software library that is written for Python in order to manipulate and analyze data. A datatype is an internal construct that is used by programming languages for understanding how to store as well as manipulate data. When a pandas data frame is made from external data, numeric columns are usually denoted as data type objects instead of int or float, causing numeric operations difficult. Let us see more details on the conversion of column type to int in the following sections.
Syntax
Following is the syntax of a type that helps in the conversion of column type to int.
DataFrame.astype(dtype, copy=True, errors='raise')
Parameters
Dtype – data type.
- dtype or python type can be used to cast whole pandas object to the matching type.
- Otherwise, use {colmn:dtype, …}, where column is the label of column and dtype is a np.dtype or python type can be used to cast one or more of the columns of dataframes to column specific types.
Copybool
- – default value is True.
- – A copy will be returned when the copy value is true.
Errors
- { ‘raise’ , ‘ignore’ } can occur.
- The default value is ‘raise’.
- Exceptions raising will be controlled on invalid data for givendtype.
- raise permits exceptions can be raised.
- ignoresuppress the chance of exceptions and the original object will be returned if any error occurs.
Return Value: Casted which is the same type as that of the caller.
How to Convert Column to Int in Pandas?
Let us see how the conversion of the column to int is done using an example.
1. Import the library pandas and set the alias name as pd
import pandas as pd
2. Define columns of the table
table = { 'Rating': [ 3.0, 4.1, 1.5, 2.77, 4.21, 5.0, 4.5 ] }
3. Set dataframe
df = pd.DataFrame(table)
4. Change the datatype of the actual dataframe into an int
df.Rating = df.Rating.astype(int)
Examples of Pandas Convert Column to Int
As already mentioned above, let us see some sample programs on the conversion of the column to int.
Example #1
Python program to demonstrate the conversion of 1 column to int.
Code:
#import the library pandas and set the alias name as pd
#import the library numpy and set the alias name as np
import pandas as pd
import numpy as np
#define columns of the employee details
Emp_details = {
'Rating': [ 3.0, 4.1, 1.5, 2.77, 4.21, 5.0, 4.5 ],
}
#data frame
df = pd.DataFrame(Emp_details)
#print the actual data frame
print("Actual DataFrame is:")
print(df)
#print the data types of actual data frame
print("datatypes of actual dataframe:")
print(df.dtypes)
#change the data type of actual data frame into int
df.Rating = df.Rating.astype(int)
#print the new data frame
print("New DataFrame is:")
print(df)
#print the data types of new data frame
print("\ndatatypes of new dataframe:")
print(df.dtypes)
Output:
In this program, first, import the pandas library and set the alias as pd. Then, define the columns of the table and print it. In order to identify the data type of objects, use “df.dtypes”. Once this is completed, change the data type of the Rating column using df.Rating.astype(int). Then, again print the column data types using “df.dtypes”. On executing the code, the original ad new data frame gets printed along with the data types.
Example #2
Python program to demonstrate the conversion of 2 columns to int.
Code:
#import the library pandas and set the alias name as pd
#import the library numpy and set the alias name as np
import pandas as pd
import numpy as np
#define columns of the employee details
Emp_details = {
'Rating': [ 3.0, 4.1, 1.5, 2.77, 4.21, 5.0, 4.5 ],
'Experience': [ 1, 3, 1, 3, 5, 3, 3 ],
}
#data frame
df = pd.DataFrame(Emp_details)
#print the actual dataframe
print("Actual DataFrame is:")
print(df)
#print the data types of actual data frame
print("data types of actual data frame:")
print(df.dtypes)
#change the data type of actual data frame into int
df.Rating = df.Rating.astype(int)
#print the new data frame
print("New DataFrame is:")
print(df)
#print the datatypes of new dataframe
print("\ndatatypes of new dataframe:")
print(df.dtypes)
Output:
In this program also, first, import the pandas library and set the alias as pd. Then, define the columns of the table and print them. In order to identify the data type of objects, use “df.dtypes”. Here, it can be seen that the type of the second column is already int. Once this is completed, change the data type of the Rating column using df.Rating.astype(int). Then, again print the column data types using “df.dtypes”. This time, both the data types will be int. On executing the code, the original ad new data frame gets printed along with the data types.
Example #3
Python program to demonstrate the conversion of columns to int.
Code:
#import the library pandas and set the alias name as pd
#import the library numpy and set the alias name as np
import pandas as pd
import numpy as np
#set the dataframe
df = pd.DataFrame([ [ "11", "22" ], [ "33", "44"] ], columns = ["aa", "bv"] )
#print the datatype
print(df.dtypes)
#set the type of a as int
df["aa"] = df["aa"].astype(str).astype(int)
#set the type of b as int
df["bv"] = df["bv"].astype(str).astype(int)
#print the datatype after changing
print(df.dtypes)
Output:
Import the pandas library and set the alias as pd. Then, set the data frame unlike the above programs, and print the data type of objects using “df.dtypes”. Here, on executing the code, it can be seen that both the data types are not available at first. Once this is completed, change the datatype of both the columns using df.Rating.astype(int). Then, again print the column datatypes using “df.dtypes”. This time, both the datatypes will be int.
Conclusion
When a pandas data frame is made from external data, numeric columns are usually denoted as data type object instead of int or float, causing numeric operations difficult. In this article, different details on conversion of the column to int such as working and examples are discussed in detail.
Recommended Articles
We hope that this EDUCBA information on “Pandas Convert Column to Int” was beneficial to you. You can view EDUCBA’s recommended articles for more information.