profile

Learn Data Science from Data School ๐Ÿ“Š

Tuesday Tip #38: Give your pandas (columns) new names ๐Ÿผ

Published 23 days 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 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 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, Do any of these sound like you? You're new to the pandas library and you want to learn the fundamentals You have some experience with pandas, but you want to fill in the gaps in your knowledge You want to learn the best practices for data analysis with pandas in 2024 If so, you should enroll in my FREE course (launching today!), pandas in 30 days. Why learn pandas? pandas is a powerful, open source Python library for data analysis, manipulation, and visualization. If you're working...

9 days agoย โ€ขย 1 min read

Hi Reader, There's a gift for you somewhere in this email... just look for the ๐ŸŽ emoji! Tip #39: Six quick Python tricks Here's what I'll cover below: Return the number of unique values Count values with Counter Better debugging with f-strings Return multiple values from a function Count while looping Create a dictionary with a comprehension Let's get started! ๐Ÿ‘‡ 1๏ธโƒฃ Return the number of unique values Need to know the number of unique values in an iterable? Convert it to a set and check the...

16 days agoย โ€ขย 2 min read

Hi Reader, How's your January going? I've been playing a lot of Pokรฉmon (the card game) with my 7 year-old... actually I'm the one who's always bugging him to play ๐Ÿ˜‚ Are you a Pokรฉmon fan? Meowscarada ex, anyone? Link of the week Introduction to Polars (Practical Business Python) Have you heard of Polars? In short, it's a high-performance, memory-efficient alternative to pandas. If you're new to Polars, this blog post walks through basic Polars code and compares it to pandas. Tip #37:...

about 1 month agoย โ€ขย 1 min read
Share this post