{"id":1210,"date":"2023-12-27T13:00:00","date_gmt":"2023-12-27T13:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1210"},"modified":"2023-12-29T08:49:24","modified_gmt":"2023-12-29T08:49:24","slug":"pandas-convert-column-to-int","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/pandas-convert-column-to-int\/","title":{"rendered":"Convert the data type of Pandas column to int (with code)"},"content":{"rendered":"\n<p>The Pandas library in Python is great for data analysis, where one common task is converting a column to the integer data type. The conversion in Python is handled easily by the <a href=\"https:\/\/favtutor.com\/articles\/pandas-dataframe-astype-method\/\">astype<\/a> function. In this article, we will explore how to use it to convert single or multiple-column types to integers in Pandas.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to convert Pandas column type to int?<\/strong><\/h2>\n\n\n\n<p>This conversion is required in many situations, such as if you need to perform mathematical operations on a column, to make the column sortable or filterable, making it easier for data visualization and statistical analysis.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using the astype() function<\/strong><\/h2>\n\n\n\n<p>The astype() function handles all sorts of conversions. It provides easy, fast efficient data type conversions. It is the primary function used in Pandas to convert the data type of a column. It allows you to specify the desired data type for a column and applies the conversion accordingly.\u00a0<\/p>\n\n\n\n<p>Let us look at an example of how to use the astpye() function to change a column into an integer data type:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Creating a DataFrame\ndata = {'Roll Number': [46.0, 35.0, 42.0],\n        'Age': [25.1 , 30.2 , 22.2]}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Using the astype() method\ndf = df.astype('int')\n\n# Display the new DataFrame\nprint('New DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    Roll Number   Age\n0         46.0  25.1\n1         35.0  30.2\n2         42.0  22.2\n\nNew DataFrame:\n    Roll Number  Age\n0           46   25\n1           35   30\n2           42   22\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Converting a Single Column to Int<\/strong><\/h3>\n\n\n\n<p>To convert a single column to the integer data type, we can use the astype() method.\u00a0First, access the column using the column name and then apply the conversion.\u00a0Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Creating a DataFrame\ndata = {'Roll Number': [46.0, 35.0, 42.0],\n        'Age': [25.1 , 30.2 , 22.2]}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Using the astype() method\ndf['Age'] = df['Age'].astype('int')\n\n# Display the new DataFrame\nprint('New DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    Roll Number   Age\n0         46.0  25.1\n1         35.0  30.2\n2         42.0  22.2\n\nNew DataFrame:\n    Roll Number  Age\n0         46.0   25\n1         35.0   30\n2         42.0   22\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Converting Multiple Columns to Int<\/strong><\/h3>\n\n\n\n<p>We can also use the astype() method to convert multiple columns to integer data type in a DataFrame. We can use the astype() method with a dictionary. The keys of the dictionary represent the column names, and the values represent the desired data types. Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Creating a DataFrame\ndata = {'Roll Number': [46.0, 35.0, 42.0],\n        'Age': [25.1 , 30.2 , 22.2]}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Using the astype() method\nconvert_dict = {'Roll Number': int, 'Age': int}\ndf = df.astype(convert_dict)\n\n# Display the new DataFrame\nprint('New DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    Roll Number   Age\n0         46.0  25.1\n1         35.0  30.2\n2         42.0  22.2\n\nNew DataFrame:\n    Roll Number  Age\n0           46   25\n1           35   30\n2           42   22\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Handling Missing Values during Conversion<\/strong><\/h3>\n\n\n\n<p>We should always make sure to handle the missing or NaN values before any conversion.\u00a0We can use the <a href=\"https:\/\/favtutor.com\/articles\/pandas-fillna-method\/\">fillna<\/a> method to handle the NaN or NULL values. <strong>The missing values can cause errors during conversions. Hence, it is advisable to take care of them before starting any conversion.<\/strong><\/p>\n\n\n\n<p>Here is a simple example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\n\n# Creating a DataFrame\ndata = {'Roll Number': [46.0, 35.0, 42.0],\n        'Age': [25.1 , 30.2 , None]}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Using fillna to handle NaN values\ndf[&quot;Age&quot;] = df[&quot;Age&quot;].fillna(0).astype(int)\n\n# Using the astype() method\nconvert_dict = {'Roll Number': int, 'Age': int}\ndf = df.astype(convert_dict)\n\n# Display the new DataFrame\nprint('New DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    Roll Number   Age\n0         46.0  25.1\n1         35.0  30.2\n2         42.0   NaN\n\nNew DataFrame:\n    Roll Number  Age\n0           46   25\n1           35   30\n2           42    0\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we learned about the various methods we can use to convert a column type to an integer in Pansas. We discussed using the astype() method to convert single as well as multiple columns to integer data type in a DataFrame.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand how to convert the data type of Pandas column to int using astype function and how to handle missing values during conversion.<\/p>\n","protected":false},"author":10,"featured_media":1212,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[37],"class_list":["post-1210","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-pandas"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1210"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1210\/revisions"}],"predecessor-version":[{"id":1255,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1210\/revisions\/1255"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1212"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}