Daily Dose of Data Science

Daily Dose of Data Science

Share this post

Daily Dose of Data Science
Daily Dose of Data Science
Alter the Datatype of Multiple Columns at Once
Copy link
Facebook
Email
Notes
More

Alter the Datatype of Multiple Columns at Once

Avi Chawla's avatar
Avi Chawla
Oct 14, 2022
4

Share this post

Daily Dose of Data Science
Daily Dose of Data Science
Alter the Datatype of Multiple Columns at Once
Copy link
Facebook
Email
Notes
More
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.

Chandu Chowdary's avatar
shayal's avatar
Lucas Nobre Barbosa's avatar
4 Likes
4

Share this post

Daily Dose of Data Science
Daily Dose of Data Science
Alter the Datatype of Multiple Columns at Once
Copy link
Facebook
Email
Notes
More
Share

Discussion about this post

User's avatar
FREE Daily Dose of Data Science PDF
Collection of posts on core DS/ML topics.
Apr 20, 2023 • 
Avi Chawla
566

Share this post

Daily Dose of Data Science
Daily Dose of Data Science
FREE Daily Dose of Data Science PDF
Copy link
Facebook
Email
Notes
More
22
15 DS/ML Cheat Sheets
Single frame summaries of must-know DS/ML concepts and techniques.
Sep 22, 2024 • 
Avi Chawla
121

Share this post

Daily Dose of Data Science
Daily Dose of Data Science
15 DS/ML Cheat Sheets
Copy link
Facebook
Email
Notes
More
You Will NEVER Use Pandas’ Describe Method After Using These Two Libraries
Generate a comprehensive data summary in seconds.
Feb 6, 2024 • 
Avi Chawla
229

Share this post

Daily Dose of Data Science
Daily Dose of Data Science
You Will NEVER Use Pandas’ Describe Method After Using These Two Libraries
Copy link
Facebook
Email
Notes
More
14

Ready for more?

© 2025 Avi Chawla
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More

Create your profile

User's avatar

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.