

Discover more from Daily Dose of Data Science
High-quality insights on Data Science and Python, along with best practices — shared daily.
Get a free 550+ page data science PDF guide and 450+ practice questions notebook.
Over 49,000 subscribers
Continue reading
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