Daily Dose of Data Science

Share this post

Alter the Datatype of Multiple Columns at Once

www.blog.dailydoseofds.com

Alter the Datatype of Multiple Columns at Once

Avi Chawla
Oct 14, 2022
2
Share

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.

2
Share
Previous
Next
Comments
Top
New
Community

No posts

Ready for more?

Β© 2023 Avi Chawla
Privacy βˆ™ Terms βˆ™ Collection notice
Start WritingGet the app
SubstackΒ is the home for great writing