Practice advanced data shaping techniques by working with Python functions, lambdas, and the apply method, essential tools for manipulating and exploring data. Gain hands-on experience creating custom functions and applying them across data structures to streamline your analysis.
Key Insights
- Learn how to define reusable Python functions using the def keyword, including setting up input variables and returning values with the return statement.
- Understand how lambdas and the apply method can be used to perform efficient, element-wise operations on data within a pandas DataFrame.
- Noble Desktop introduces foundational and advanced applications of Python functions and lambdas, helping users enhance their data analysis capabilities.
This lesson is a preview from our Data Analytics Certificate Online (includes software). Enroll in this course for detailed lessons, live instructor support, and project-based training.
Let's do some more in-depth data shaping. So what do I mean by that? I mean, we're going to be doing some data manipulation, exploring our data and changing it, using some more advanced tools and doing some more intricate work with it. To do that, we'll use a lambdas and apply.
And if you're not super familiar with lambdas, well, I will get you more familiar with them. I'll give you a gentle introduction to them. And if, on the other hand, you know your lambdas, but you're not well-practiced with them, and in my experience, data scientists, myself included, tend not to use these advanced tools quite as often, so we're not as well-versed in them, you could probably use some practice.
Once we are working on that, we'll be doing all of this in our notebook so we can practice and explore our data before we go back to working locally. Okay, let's first explore functions and refresh ourselves on those. So, let's define a function that adds five to the input number.
The way we're going to do that is we're going to use the def keyword for defining a function. And we name the function, I'm going to name this one addFive and we name our input variable. I'm going to call it num.
And this variable works very similar to a loop. I want you to think about a loop for num in nums. Anything like that, num is a variable that we don't give a value to, but it gets a new value each time through the loop thanks to the loop structure.
And it's the same thing here with this function. Every time we run this code, this code block, num will get a value, we get a different value that we've given it. Let's see what we're going to do with that.
So, this is a very simple function. We'll evaluate num plus five. And again, num will be a different value.
Maybe we'll pass in 10, maybe we'll pass in who knows what. And to evaluate this, to output this, as what addFive evaluates to, we use the return keyword. Okay.
So, now, hit return after this and you notice that this code block, assuming you had a colon there, automatically gets indented to be part of the function. And as you're going down below the functions definition, you should once again be unindented, de-dented back flush to the left of the document. So, we will call addFive.
I'll print it first, addFive, and we'll pass in 10. And if we execute that, addFive 10 comes out to 15. Now, that's because this time through the function, when we define the function, num didn't have a value.
It was just like, hey, in general, here's a set of instructions for adding five to a number. Then when we get down here, we actually execute those instructions and we give it a set value, a specific value, in this case, 10. And then this time through this function call, num has the value 10.
It's as if Python did this, num equals 10, just for this time through the function call, not adding this actual code to our function, but just this time through, num gets the value 10, so that when we evaluate num plus five, it's 10 plus five, or 15. And then the return makes that 15 actually come out here. addFive 10 needs to evaluate to something so we can print something.
We can't print the words addFive 10. I guess we could if we put some quotes in here, but why would we do that? We don't want literally that. We want it to evaluate the Python addFive 10, figure out what that function call returns and print that.
So this will evaluate to, because of the return statement above, this will evaluate to 15, and that's how we get this 15 printing. Let's try that one more time. Let's say print addFive to .25. And now, again, first, when we run this code, first it will define what addFive does, but without actually executing any of this code.
And then we will say, okay, now call addFive with 10. That will evaluate to 15, and it will print 15. And then it will come back down here and say, run the addFive function with 25.
So num becomes 25 the second time through, and we evaluate 25 plus five. It returns that, meaning it comes back out of the function and says, okay, this is 25 then, or rather, this is 30. Let's try that.
Execute it. Yep, it printed out 15 when it ran it the first time, and it printed out 30 when it ran it the second time. Okay, that's our gentle introduction to functions or reintroduction for many of us.
We're going to work some more on functions, then we're going to try them with a data frame and an apply method.