Formatting Numbers with Python: Using Apply and Lambda

Format numeric values as currency strings with dollar signs and commas using a custom function or lambda with apply.

Format currency values in a DataFrame using Python's apply function and formatted string literals to enhance readability with dollar signs and commas. Learn how to implement both custom functions and lambda expressions for cleaner and more professional data presentation.

Key Insights

  • Create a reusable format_currency function using f-strings and include commas to convert numeric values into properly formatted dollar amounts (e.g., $50,000,000).
  • Apply the formatting function across a DataFrame column using the .apply() method, transforming all numeric entries into readable currency strings.
  • Use lambda expressions within .apply() as an alternative to named functions for concise, inline formatting of numeric data in pandas.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

One possible solution to our basic challenge here is to say, well first we can define a function, maybe format currency takes in takes in a dollar number and it returns an f-string where we put in the literal dollar sign and our value for dollar number. And we can test it out. Format currency 3 becomes the string dollar sign 3. Format currency 5 becomes a dollar 5. Now if it's 5,000 it was dollar 5,000 without the commas.

And that's a particular problem if you get many zeros. Those aren't zeros, those are. And it's like, well how is that 50 million? Is that 500,000? Is that 500 million? Don't know.

Just sit there and count the zeros. With some commas you wouldn't have to. We can improve this by saying dollar number and if you know your format, and if you don't there's a lot of great resources online for how to do this number-to-string formatting, but you can say comma here like that.

And that should work, although I'm never quite positive. I often have to look this up. Again, you know, that's a, that's part of being an engineer.

But it did work. It was 50 million. Okay, that was my guess.

I should have guessed. You would have known that I'm so good at eyeballing numbers. Okay, so we could do this or and now that we know it works, we can make our numbers formatted the same way.

Our numbers column should be our numbers column dot apply our format currency function. Then we can look at our data frame. And there it is.

It all got formatted. Now if I run this again, I get an error. Can I specify comma with string? Fair enough.

So, however, if we do, we're gonna run it again, we could do run all. And that runs it from the start and then up to this point they are still numbers and it can apply that that function to it to convert a string, a number to a string. Okay, now if we want to do this as a lambda instead, we could say df numbers equals df numbers dot apply a lambda that takes in a number.

Nope, a lambda that takes in a number and returns an f string, the literal dollar sign, and then the number formatted with a comma. And if we run all again, because I run this again, it will be an error. And there we go, it worked.

They have dollar sign and commas and they're all very nice strings. Which makes numbers probably not the best column anymore. Okay, so that's our gentle introduction to apply.

Next we'll work with some real or at least real-ish data. And try to use, see how we can use this.

Yelp Facebook LinkedIn YouTube Twitter Instagram