{"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. 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 Parameters<\/strong><\/p>\n dtype:<\/strong> The data type that should be applied to the entire data frame. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Now, apply the astype() method on the ‘Name’ column to change the data type to ‘category’<\/p>\n Output:<\/strong><\/p>\n Note:<\/p>\n Use the pandas.read csv() function to import the dataset. The dataset can be found here.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Now attempt to change the datatype of the variables ‘name’ and ‘fat’ to string, float64 respectively. As a result, we can say that the astype() function allows us to change the data types of multiple columns in one go.<\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"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. 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
\nThis is when data column conversion comes into play.<\/p>\nDataFrame.astype(dtype, copy=True, errors='raise')<\/pre>\n
\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>\n1)<\/strong>astype() – with DataFrame<\/h4>\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
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
# 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
ID int64\r\nName category\r\nsalary int64\r\ndtype: object<\/pre>\n
You can also change to datatype 'string'<\/pre>\n
2)astype() Method – with a Dataset in Python<\/strong><\/h4>\n
\n
# Import pandas library using the import keyword\r\nimport pandas\r\n# Import some random dataset using the pandas.read_csv() function by passing\r\n# the filename as an argument to it.\r\n# Store it in a variable.\r\ncereal_dataset = pandas.read_csv(\"cereal.csv\")\r\n# Apply dtypes to the above dataset\r\ncereal_dataset.dtypes\r\n<\/pre>\n
name object\r\nmfr object\r\ntype object\r\ncalories int64\r\nprotein int64\r\nfat int64\r\nsodium int64\r\nfiber float64\r\ncarbo float64\r\nsugars int64\r\npotass int64\r\nvitamins int64\r\nshelf int64\r\nweight float64\r\ncups float64\r\nrating float64\r\ndtype: object<\/pre>\n
# Import pandas library using the import keyword\r\nimport pandas\r\n# Import some random dataset using the pandas.read_csv() function by passing\r\n# filename as an argument to it.\r\n# Store it in a variable.\r\ncereal_dataset = pandas.read_csv(\"cereal.csv\")\r\n# Change the datatype of the variables 'name' and 'fat'using the astype() function\r\nprint(\"The dataset after changing datatypes:\")\r\ncereal_dataset = cereal_dataset.astype({\"name\":'string', \"fat\":'float64'}) \r\n# Apply dtypes to the above dataset\r\ncereal_dataset.dtypes\r\n\r\n<\/pre>\n
The dataset after changing datatypes:\r\nname string\r\nmfr object\r\ntype object\r\ncalories int64\r\nprotein int64\r\nfat float64\r\nsodium int64\r\nfiber float64\r\ncarbo float64\r\nsugars int64\r\npotass int64\r\nvitamins int64\r\nshelf int64\r\nweight float64\r\ncups float64\r\nrating float64\r\ndtype: object<\/pre>\n