Hi [FIRST NAME GOES HERE],
My goal with Tuesday Tips is to help you get better at Data Science every week.
Is there anything that would make these tips even more helpful for you? Let me know! ๐ฌ
You can find past tips at tuesday.tips. (Yes, thatโs a real URL!)
Let's say that we have a simple pandas DataFrame:
I prefer to use dot notation to select pandas columns, but that won't work since the column names have spaces. Let's fix this!
The most flexible method for renaming columns is the rename() method. You pass it a dictionary in which the keys are the old names and the values are the new names:
The best thing about this method is that you can use it to rename any number of columns, whether it's just one column or all columns.
Now if you're going to rename all of the columns at once, a simpler method is just to overwrite the columns attribute of the DataFrame:
If the only thing you're doing is replacing spaces with underscores, an even better method is to use the str.replace() method, since you don't have to type out all of the column names:
All three of these methods have the same result, which is to rename the columns so that they don't have any spaces:
Finally, if you just need to add a prefix or suffix to all of your column names, you can use the add_prefix() method:
Or the add_suffix() method:
If you have some pandas experience and want more tips like this, check out My top 25 pandas tricks ๐ผ๐คนโโ๏ธ on YouTube.
If you're new to pandas, I'm excited to announce that my new course, pandas in 30 days, will available soon!
Did you like this weekโs tip? Please send it to a friend or share this link on social. It really helps me out! ๐
See you next Tuesday!
- Kevin
P.S. The SAT Question Everyone Got Wrong (YouTube)
Did someone AWESOME forward you this email? Sign up here to receive Data Science tips every week!
Join 25,000+ intelligent readers and receive AI tips every Tuesday!
Hi Reader, Yesterday, I posted this announcement on LinkedIn and Bluesky and X: Kevin Markham @justmarkham Dream unlocked: I'm publishing my first book! ๐๐๐ It's called "Master Machine Learning with scikit-learn: A Practical Guide to Building Better Models with Python" Download the first 3 chapters right now: ๐ https://dataschool.kit.com/mlbook ๐ Thanks for your support ๐ 1:47 PM โข Sep 11, 2025 1 Retweets 5 Likes Read 1 replies This has been a dream of mine for many years, and I'm so excited...
Hi Reader, Hope youโve had a nice summer! โ๏ธ As for me, Iโve been finishing my first ever book! I canโt wait to tell you about it and invite you to be part of the launchโฆ stay tuned ๐ Today's email focuses on a single important topic: AIโs impact on your mental health ๐ง Read more below! ๐ Sponsored by: Morning Brew The 5-Minute Newsletter That Makes Business Make Sense Business news doesn't have to be dry. Morning Brew gives you the biggest stories in business, tech, and finance with quick...
Hi Reader, Most of us access Large Language Models (LLMs) through a web interface, like ChatGPT or Claude. Itโs highly convenient, though there are two potential drawbacks: Cost: Some amount of usage is free, but heavy usage (or access to premium models) costs money. Privacy: Depending on the service, your chats may be used to train future models. (Or at the very least, your chats may be accessed if ordered by a court.) One solution is to run an LLM locally, which has gotten much easier with...