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

Published about 1 year ago • 1 min read

Hi Reader!

In case you missed last week's announcement, **I'll be sharing a new data science tip with you every Tuesday!**

Python introduced f-strings back in version 3.6 (six years ago!), but I've only recently realized how useful they can be.

Let's start with some simple examples of how they're commonly used, and then I'll end with a real-world example (using pandas).

To make an f-string, you simply put an "f" in front of a string. By putting the "name" and "age" objects inside of curly braces, those objects are automatically substituted into the string.

Strings have an upper() method, and so I was able to call that method on the "role" string from within the f-string.

You can evaluate an expression (a math expression, in this case) within an f-string.

This looks much nicer, right? The colon begins the format specification, and the ".1%" means "format as a percentage with 1 digit after the decimal point."

🔗 Guide to f-strings (written by my pal Trey Hunner)

🔗 f-string cheat sheet (also by Trey)

Recently, I was analyzing the survey data submitted by 500+ Data School subscribers. I asked each person about their **level of experience** with 11 different data science topics/tools, plus their **level of interest** in improving those skills this year.

Thus I had 22 columns of data, with names like **“python_experience”**, **“python_interest”**, **“pandas_experience”**, **“pandas_interest”**, etc.

Each **“experience”** column was coded from 0 (None) to 3 (Advanced), and each **“interest”** column was coded from 0 (Not interested) to 2 (Definitely interested).

Among other things, I wanted to know the **mean level of interest in each topic**, as well as the **mean level of interest in each topic by experience level**.

Here's what I did to answer those questions:

(The “cats” list actually had 11 categories, so the loop allowed me to examine all of the categories at once.)

Notice how I used f-strings:

🧵 Because of the naming convention, I could access the DataFrame columns using **df[f'{cat}_interest']** and **df[f'{cat}_experience']**

🧵 I capitalized the category using **f'{cat.upper()}'** to help it stand out

🧵 I formatted the mean interest to 2 decimal places using **f'{mean_interest:.2f}'**

*How helpful was today’s tip?*

**Do you have a favorite use for f-strings?** Click reply and let me know!

See you next Tuesday!

- Kevin

**P.S.** Did someone awesome forward you this email? Sign up here to receive data science tips every week!

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...

8 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...

15 days ago • 2 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...

22 days ago • 1 min read