Articles

convert column to int in pandas | astype in pandas

convert column to int in pandas | astype in pandas


In this pandas tutorial, we will discuss about:

  • datatype of dataframe pandas,
  • convert column to int in pandas,
  • astype in pandas

Lets first understand about dataframe in pandas.

 

DataFrame in pandas is a 2-dimensional data structure that store data in 2-dimensional format. One of the dimension refers to a row and second dimension refers to a column. So it generally store data in rows and columns.

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key.

 

Example: Create DataFrame and get datatype

In this example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.

 

We can get the datatype of dataframe pandas using dtypes method.

Syntax:

dataframe.dtypes

Lets create dataframe and also print the datatype of dataframe pandas:

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":['1200','3422','5644','670']

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

# display the datatypes
print(data.dtypes)

Outputdatatype of dataframe pandas is printed below

      college_id       college_name college_address Total Staff
one        c-001  vignan university          guntur        1200
two        c-021               vvit          guntur        3422
three      c-002           RVR - JC          guntur        5644
four       c-004  Andhra University          guntur         670
college_id         object
college_name       object
college_address    object
Total Staff        object
dtype: object

Now lets proceed to convert column to int in pandas.


Method 1convert column to int in pandas using astype()

This method astype in pandas will take dataframe column as input and convert the data type to integer.

 

We can convert to integer by specifying a keyword called 'int'.

Syntax:

dataframe['column'].astype(int)

where,

1. dataframe is the input dataframe

2. column is the name of the column in which the datatype to be converted.

 

Note - In pandas DataFrame, the string type column will be considered as an object.

Exampleconvert column to int pandas

In this convert column to int in pandas example, we will convert Total Staff column to integer type

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":['1200','3422','5644','670']

                   },index=['one','two','three','four'])

# convert Total Staff column to integer type
data['Total Staff']=data['Total Staff'].astype(int)

# display the datatypes
print(data.dtypes)

Output: Result for convert column to int in pandas is shown below for total staff column.

college_id         object
college_name       object
college_address    object
Total Staff         int64
dtype: object

From the output we observed that  data type of Total Staff column is object before  and now it is converted to integer.


Method 2convert column to int pandas using astype() with dictionary

This method astype in pandas will take dataframe column as input and convert the data type to integer.

 

We can convert to integer by specifying a keyword called 'int'. astype() will take column name inside a dictionary, such that key will be the column name and value will be the int keyword.

Syntax:

dataframe.astype({"column":int})

where,

1. dataframe is the input dataframe

2. column is the name of the column in which the datatype to be converted.

 

Note - In pandas DataFrame, the string type column will be considered as an object.

Exampleastype in pandas example

In this convert column to int pandas example, we will convert Total Staff column to integer type

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":['1200','3422','5644','670']

                   },index=['one','two','three','four'])

# convert Total Staff column to integer type
data = data.astype({"Total Staff": int})

# display the datatypes
print(data.dtypes)

Output: convert column to int in pandas result

college_id         object
college_name       object
college_address    object
Total Staff         int64
dtype: object

From the output we observed that  data type of Total Staff column is object before  and now it is converted to integer.


Method 3 : Using astype() by specifying the type

This method astype in pandas will take dataframe column as input and convert the data type to integer .

We can convert to integer by specifying a keyword called 'int'.But we have to specify the old datatype of the column to be converted.

Syntax:

dataframe['column'].astype(old_datatype).astype(int)

where,

1. dataframe is the input dataframe

2. column is the name of the column in which the datatype to be converted.

3. old_datatype is the datatype of the column (like str,float etc)

 

Note - In pandas DataFrame, the string type column will be considered as an object.

Exampleastype in pandas example by specifying type

In this convert column to int in pandas example, we will convert Total Staff column to integer type

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":['1200','3422','5644','670']

                   },index=['one','two','three','four'])

# convert Total Staff column to integer type
data['Total Staff'] = data['Total Staff'].astype(str).astype(int)

# display the datatypes
print(data.dtypes)

Output: convert column to int in pandas result is given below

college_id         object
college_name       object
college_address    object
Total Staff         int64
dtype: object

From the output we observed that data type of Total Staff column is object before and now it is converted to integer.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 15,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!