This content originally appeared on CodeSource.io and was authored by Codesource Staff
In this article, you will learn how to convert String to Int in Pandas.
A Pandas DataFrame is nothing but a two-dimensional data structure or two-dimensional array that represents the data in rows and columns. It is open-source and potent, fast, and easy to use. Basically, while working with big data we need to analyze, manipulate and update them and the pandas’ library plays a lead role there. Sometimes, we may need to convert String to Int in Pandas. To perform this action, we can use the .astype()
, or .to_numeric()
method. Both will be working perfectly. In this article, we will explore these two methods and see how we can perform this action in Pandas. First, let’s create a simple Pandas DataFrame in the below section:
import pandas as pd
student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John', 'Roy', 'Rohit'],
'Age' : ['21', '23', '19', '22', '20'],
'Marks' : ['77', '84', '90', '67', '55'],
})
print(student_df)
print(student_df.dtypes)
# Output:
# Name Age Marks
# 0 Alex 21 77
# 1 Deven 23 84
# 2 John 19 90
# 3 Roy 22 67
# 4 Rohit 20 55
# Name object
# Age object
# Marks object
# dtype: object
Here, you can see that we have taken a simple Pandas DataFrame that represents the student’s name, age, and marks. You can see their type is Object and their value is in the string form. We will convert this into an Int value in the following section.
Example One: convert String to Int in Pandas using astype()
The simplest way to perform this action in Pandas is to use the .astype()
. Follow the below code example to perform this action:
import pandas as pd
student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John', 'Roy', 'Rohit'],
'Age' : ['21', '23', '19', '22', '20'],
'Marks' : ['77', '84', '90', '67', '55'],
})
student_df['Age'] = student_df['Age'].astype(int)
print(student_df.dtypes)
# Output:
# Name object
# Age int32
# Marks object
# dtype: object
Here, you can see that the type of Age
column was Object and its value was the string. We use the .astype()
method to convert it into an integer. You can see the result in the output that the type has been changed to the int.
Example Two: convert String to Int in Pandas using to_numeric()
There is another way of performing this action. We can use the to_numeric()
method. Follow the below code example to use this method:
import pandas as pd
student_df = pd.DataFrame({'Name' : ['Alex', 'Deven', 'John', 'Roy', 'Rohit'],
'Age' : ['21', '23', '19', '22', '20'],
'Marks' : ['77', '84', '90', '67', '55'],
})
student_df['Marks'] = pd.to_numeric(student_df['Marks'])
print(student_df.dtypes)
# Output:
# Name object
# Age object
# Marks int64
# dtype: object
This time you can see that the Marks
column value has been changed to Integer Value. These are some useful methods that you may follow to perform this action in Pandas.
This content originally appeared on CodeSource.io and was authored by Codesource Staff
Codesource Staff | Sciencx (2022-10-03T15:22:28+00:00) How to convert String to Int in Pandas. Retrieved from https://www.scien.cx/2022/10/03/how-to-convert-string-to-int-in-pandas-3/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.