Tuesday Tip #38: Give your pandas (columns) new names 🐼


Hi Reader,

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!)


👉 Tip #38: Five ways to rename your DataFrame columns

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:


📈 Going further

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!


👋 Until next time

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!

Learn Artificial Intelligence from Data School 🤖

Join 25,000+ intelligent readers and receive AI tips every Tuesday!

Read more from Learn Artificial Intelligence from Data School 🤖

Hi Reader, Here are your top AI stories for the week: ChatGPT can weaken your brain Claude shares nerve gas recipe Amsterdam ends AI experiment due to bias Read more below! 👇 Sponsored by: Brain.fm Transform Your Focus With Brain.fm I know you're always on the hunt for tools that genuinely improve your life—which is why I'm excited to introduce you to Brain.fm's groundbreaking focus music. Brain.fm's patented audio technology was recently validated in a top neuroscience journal, showing how...

Hi Reader, Last week, I invited you to help me test Google's Data Science Agent in Colab, which promises to automate your data analysis. Does it live up to that promise? Let's find out! 👇 Sponsored by: Morning Brew Business news you’ll actually enjoy Join 4M+ professionals who start their day with Morning Brew—a free daily newsletter that makes business, tech, and finance news genuinely enjoyable to read and hard to forget. Each morning, it breaks down complex stories in plain English—cutting...

Hi Reader, Today I'm trying something brand new! I wrote short summaries of the 5 most important AI stories this week, and also turned it into a video: Watch the video I'd love to know what you think! 💬 AI-generated TV ad airs during NBA finals Prediction market Kalshi just aired this AI-generated ad on network TV during the NBA finals. It was created in just two days by one person using Google's new Veo 3 video model, plus scripting help from Google's Gemini chatbot. Expect to see many more...