Utilizing Lambda Functions and Apply for Data Transformation

Explore how to use both named and anonymous lambda functions with apply() to modify DataFrame values, including formatting numbers as dollar strings.

Learn how to use anonymous functions with Python’s apply method to streamline data transformations without defining named functions. This article illustrates the flexibility of lambda expressions when modifying DataFrame values directly and effectively.

Key Insights

  • Lambda functions can be passed directly to the apply() method in pandas to perform operations—such as adding a value or formatting numbers—without assigning the function to a variable.
  • Replacing named functions with lambdas allows for more concise code, especially for simple, one-time use operations like adding a fixed number or applying random adjustments to data.
  • Noble Desktop demonstrates how to apply both named and anonymous functions to format numerical data as strings with currency symbols and proper comma placement, while highlighting potential issues when executing formatting code multiple times.

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.

Let's try doing this with some lambdas. So the same way that we can make a variable equal this function, that sort of drives home that this here is a value. It's a value that we can not only assign a variable to, but we can also pass it as an argument.

Just as we're passing it here, whether it's a named function, whether it's a regular function or a lambda function, we're passing its value to apply. And apply is taking those sets of instructions and applying them to every single element in the list. In the same way, we can do it without it being a variable, without it being a named function, but just pass the value of the function itself.

And here's how we do that with a lambda. We can say, I can comment this out, and I can say dfNumbers.apply. Actually, if we want to make this change permanent, dfNumbers equals dfNumbers.apply. We pass in lambda num plus five. That's the same thing as doing the add five, because this is the value that the add five variable here holds.

It holds the instructions lambda takes in a num, returns num plus five. And so instead of saving it in a variable and then passing it in, we can do this. This is exactly like saying, instead of saying greeting equals hello and saying print greeting, like, okay, well, why do we need, if we know in our code the value of this function, value of this variable, instead of saying print out greeting and having it go look up hello in its memory, we'll just say print hello.

Same exact result, because greeting holds the value hello. If we know the value the variable holds, we could just put that value right in here. In the exact same way, instead of saying apply add five, add five holds this value that's right here in this code, we can say, okay, we'll apply that value, apply that lambda to every value.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

And in the same way, we can do, we could then do dfNumbers equals dfNumbers.apply lambda dollars, dollar amount to round dollar amount plus random.random to two places. And we could pick, and it's very typical to do so, we can pick any variable for these. It's typical to pick a much simpler variable name for a lambda, since, you know, being succinct and short is what it's all about.

And I can be referring to this variable over and over again, apply a variable that takes in a dollar amount and returns a dollar amount plus random.random to two places. But either way we want to do it. And we can do the same thing here if we like.

Rename num to n. Take in an n, return n plus five. Let's comment out our previous version, execute this. Yep, they've all had five added and a random set amount.

Okay, let's have you do a challenge. Use a named function and apply, and then separately, for practice, a lambda function, an anonymous lambda function, more specifically, and apply to replace each number in the numbers with a string with a dollar sign. For example, 10,056 should become the dollar amount, the string, dollar sign 10,056.

10,001 and five cents should become dollar sign 10,001 and five cents. As a bonus, you can add some commas in so that it becomes $10,056 with a nice little comma for readability. And you should heed this warning here.

If you try running this code twice, you're going to get into an error because maybe you made it into this. And now if you try to make it into this, the formatting you do of adding that comma in may be an issue if you're converting it from a number. By converting from this number to this, you can't then take this and convert it in the same way from a number to a string.

And the solution for this is just, hey, if you run into that error, a value error dealing with a string, you can just do run all in your notebook to say, hey, don't run it twice. Rerun all the code above, then run it. All right.

So I'm going to give you a moment to do that. And in the next video, we'll walk through a solution to that.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram