5 Cool Python Tricks to Make Your Life Easier

Improve your productivity and code quality with these quick tips.

Felipe Mezzarana
4 min readNov 13, 2022
Photo by Julius Drost on Unsplash

Who doesn’t like a quick trick to get something done faster, more efficiently or simply more elegantly?

I made this list to share five random tricks that I learned /developed working with data and using Python every day. let’s get to it!

1 º Quickly renaming all columns

In many situations, changing column names may be a bad choice. However, when your goal is simply to analyze data or generate visualizations, using a DataFrame with columns names containing whitespace, accents or symbols can be quite annoying.

It’s easier to select columns with standardized names, not to mention when someone left that unwanted whitespace at the end of an Excel column name. So this is how we can quickly transform all columns names to snake_case pattern:

2º Creating your own modules

In the first trick we created a nice function, now we just need to copy and paste it into all the scripts we intend to use it… right?

Please, don’t! how about just importing the function exactly like you usually do with any other library? It’s easier than it looks and it can be very useful, both to make your life easier and to share your work with others.

You just need to create a python file (.py) containing your function, and copy that file to the path where Python checks for modules and packages. To find out what path Python checks, just run in your favorite IDE:

import sys
print(sys.path)

Now that you copied the file to one of the printed paths (let’s say you named it ‘my_first_module.py’) you will be able to import it like any libary:

import my_first_module

# Calling the function
my_first_module.rename_columns(any_df)

Generating fake data

Whether for testing or learning purposes, we often need to create fake variables. For example, you might need fake data to write unit tests or to play with a new library.

Of course, we can always import or create these variables from scratch, but there is a much easier and more versatile solution! I’m talking about the Faker library, with it we can generate random data of different types and characteristics, take a look:

I showed only a few examples, there is still much more to explore in the documentation, like selecting a specific language and other data options.

4º Using f-strings like a pro

If you study/work with Python you probably already use f-strings. If you still don’t know it, no problem, it’s a simple concept. Basically, using the letter “f” before the string allow variables to be inserted into the text, inside {}. Something like that:

age = 29
print(f'My age is {age}')

f-strings are awesome! But we can make them even more elegant and professional with a series of formatting options, allowing, for example, to improve user interfaces or generate better custom logs. Take a look:

5º Loading bar

Have you ever needed to run a code that goes through a long loop and was left wondering if there was still a lot of time to finish or if everything was going as expected?

Your problems are over! How about developing an elegant and reusable solution? here is how I did it:

Extra!! A function to analyze any DataFrame

There are some basic analysis that needs to be done on any imported/generated DataFrame. How about creating a function to perform these repetitive steps and just importing it (as shown in the second trick)

I’ve already developed this function for my personal use, and I thought it would be cool to make it available here for anyone who wants to use, modify, or have as a basis to create their own!

To exemplify, I will apply this function in a dataframe that contains information about customer characteristics and payment history of a credit card

credit_card_history_df = pd.read_csv('credit_card_history.csv')
analyse_df(credit_card_history_df)

That’s it! Thanks for reading, I hope you enjoyed and learned something new. If you know any other tricks or have any suggestions, please let me know in the comments!

--

--

Felipe Mezzarana

Passionate about Data! I love to learn new things and share knowledge.