Alter the Datatype of Multiple Columns at Once
A common approach to alter the datatype of multiple columns is to invoke the 𝐚𝐬𝐭𝐲𝐩𝐞() method individually for each column.
Although the approach works as expected, it requires multiple function calls and more code. This can be particularly challenging when you want to modify the datatype of many columns.
As a better approach, you can condense all the conversions into a single function call. This is achieved by passing a dictionary of column-to-datatype mapping, as shown below.
Further reading: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.astype.html
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (250+ pages) with 200+ tips.