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!)
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, This week, I've got a short tip about AI agents, followed by some Data School news... π Tip #56: What are AI agents? Google is calling 2025 "the agentic era," DeepLearning.AI says "the agentic era is upon us," and NVIDIA's founder says "one of the most important things happening in the world of enterprise is agentic AI." Clearly AI agents are a big deal, but what exactly are they? Simply put, an AI agent is an application that uses a Large Language Model (LLM) to control its...
Hi Reader, Last week, I launched a brand new course: Build an AI chatbot with Python. 120+ people enrolled, and a few have already completed the course! π Want to join us for $9? π Tip #55: Should you still learn to code in 2025? Youβve probably heard that Large Language Models (LLMs) are excellent at writing code: They are competitive with the best human coders. They can create a full web application from a single prompt. LLM-powered tools like Cursor and Copilot can autocomplete or even...
Hi Reader, The Python 14-Day Challenge starts tomorrow! Hope to see you there π€ π Tuesday Tip: My top 5 sources for keeping up with AI I'll state the obvious: AI is moving incredibly FAST π¨ Here are the best sources I follow to keep up with the most important developments in Artificial Intelligence: The Neuron (daily newsletter) My top recommendation for a general audience. Itβs fun, informative, and well-written. It includes links to the latest AI news and tools, but the real goldmine is...