You are reading the article Pandas Convert Column To Int updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Pandas Convert Column To Int
Definition of Pandas Converts Column to IntPandas 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax 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 pd2. 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 IntAs already mentioned above, let us see some sample programs on the conversion of the column to int.
Example #1Code:
#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 #2Python 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 #3Python 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.
ConclusionWhen 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 ArticlesWe 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.
You're reading Pandas Convert Column To Int
Update the detailed information about Pandas Convert Column To Int on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!