{"id":26211,"date":"2021-12-21T09:28:11","date_gmt":"2021-12-21T03:58:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=26211"},"modified":"2021-12-21T09:28:11","modified_gmt":"2021-12-21T03:58:11","slug":"python-astype-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-astype-method-with-examples\/","title":{"rendered":"Python astype() Method with Examples"},"content":{"rendered":"

In this tutorial, we will go over an important idea in detail: Data Type Conversion of Columns in a DataFrame Using Python astype() Method.<\/p>\n

Python is a superb language for data analysis, owing to its fantastic ecosystem of data-centric python programmes. Pandas is one of these packages, and it greatly simplifies data import and analysis.<\/p>\n

astype() Method:<\/strong><\/p>\n

DataFrame.astype() method is used to convert pandas object to a given datatype. The astype() function can also convert any acceptable existing column to a categorical type.<\/p>\n

We frequently come across a stage in the realm of Data Science and Machine Learning when we need to pre-process and transform the data. To be more specific, the transformation of data values is the first step toward modeling.
\nThis is when data column conversion comes into play.<\/p>\n

The Python astype<\/strong>() method allows us to convert\u00a0the data type of an existing data column in a dataset or data frame.<\/p>\n

Using the astype() function, we can modify or transform the type of data values or single or multiple columns to a completely different form.<\/p>\n

Syntax:<\/strong><\/p>\n

DataFrame.astype(dtype, copy=True, errors='raise')<\/pre>\n

Parameters<\/strong><\/p>\n

dtype:<\/strong> The data type that should be applied to the entire data frame.
\ncopy:<\/strong> If we set it to True, it makes a new copy of the dataset with the changes incorporated.
\nerrors:<\/strong> By setting it to ‘raise,’ we allow the function to raise exceptions. If it isn’t, we can set it to ‘ignore.’<\/p>\n

1)<\/strong>astype() – with DataFrame<\/h4>\n

Below is the implementation:<\/strong><\/p>\n

# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Give the dictionary as static input and store it in a variable.\r\n# (data given in the dictionary form)\r\ngvn_data = {\"ID\": [11, 12, 13, 14, 15, 16], \"Name\": [\"peter\", \"irfan\", \"mary\",\r\n                                                     \"riya\", \"virat\", \"sunny\"], \"salary\": [10000, 25000, 15000, 50000, 30000, 22000]}\r\n# Pass the given data to the DataFrame() function and store it in another variable\r\nblock_data = pd.DataFrame(gvn_data)\r\n# Print the above result\r\nprint(\"The given input Dataframe: \")\r\nprint(block_data)\r\nprint()\r\n# Apply dtypes to the above block data\r\nblock_data.dtypes\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The given input Dataframe: \r\n   ID   Name  salary\r\n0  11  peter   10000\r\n1  12  irfan   25000\r\n2  13   mary   15000\r\n3  14   riya   50000\r\n4  15  virat   30000\r\n5  16  sunny   22000\r\n\r\nID         int64\r\nName      object\r\nsalary     int64\r\ndtype: object<\/pre>\n

Now, apply the astype() method on the ‘Name’ column to change the data type to ‘category’<\/p>\n

# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Give the dictionary as static input and store it in a variable.\r\n# (data given in the dictionary form)\r\ngvn_data = {\"ID\": [11, 12, 13, 14, 15, 16], \"Name\": [\"peter\", \"irfan\", \"mary\",\r\n                                                     \"riya\", \"virat\", \"sunny\"], \"salary\": [10000, 25000, 15000, 50000, 30000, 22000]}\r\n# Pass the given data to the DataFrame() function and store it in another variable\r\nblock_data = pd.DataFrame(gvn_data)\r\n# Apply the astype() method on the 'Name' column to change the data type to 'category'\r\nblock_data['Name'] = block_data['Name'].astype('category')\r\n# Apply dtypes to the above block data\r\nblock_data.dtypes<\/pre>\n

Output:<\/strong><\/p>\n

ID           int64\r\nName      category\r\nsalary       int64\r\ndtype: object<\/pre>\n

Note:<\/p>\n

 You can also change to datatype 'string'<\/pre>\n

2)astype() Method – with a Dataset in Python<\/strong><\/h4>\n

Use the pandas.read csv() function to import the dataset. The dataset can be found here.<\/p>\n

Approach:<\/strong><\/p>\n