Learn Data Science from Data School πŸ“Š

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

Published 4 months agoΒ β€’Β 1 min read

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 (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 Data Science from Data School πŸ“Š

Kevin Markham

Join 25,000+ aspiring Data Scientists and receive Python & Data Science tips every Tuesday!

Read more from Learn Data Science from Data School πŸ“Š

Hi Reader, I'm really proud of this week's tip because it covers a topic (data leakage) that took me years to fully understand. 🧠 It's one of those times when I feel like I'm truly contributing to the collective wisdom by distilling complex ideas into an approachable format. πŸ’‘ You can read the tip below πŸ‘‡ or on my blog. πŸ”— Link of the week Building an AI Coach to Help Tame My Monkey Mind (Eugene Yan) In this short post, Eugene describes his experiences calling an LLM on the phone for coaching:...

about 2 months agoΒ β€’Β 4 min read

Hi Reader, Last week, I recorded the FINAL 28 LESSONS πŸŽ‰ for my upcoming course, Master Machine Learning with scikit-learn. That's why you didn't hear from me last week! πŸ˜… I edited one of those 28 videos and posted it on YouTube. That video is today's tip, which I'll tell you about below! πŸ‘‰ Tip #45: How to read the scikit-learn documentation In order to become truly proficient with scikit-learn, you need to be able to read the documentation. In this video lesson, I’ll walk you through the five...

about 2 months agoΒ β€’Β 1 min read

Hi Reader, happy Tuesday! My recent tips have been rather lengthy, so I'm going to mix it up with some shorter tips (like today's). Let me know what you think! πŸ’¬ πŸ”— Link of the week A stealth attack came close to compromising the world's computers (The Economist) If you haven't heard about the recent "xz Utils backdoor", it's an absolutely fascinating/terrifying story! In short, a hacker (or team of hackers) spent years gaining the trust of an open-source project by making helpful...

2 months agoΒ β€’Β 1 min read
Share this post