Locked lesson.
About this lesson
The apply method allows us to create our own functions and apply them to the data in our columns.
Exercise files
Download this lesson’s related exercise files.
Operations: Apply Method.docx57.1 KB Operations: Apply Method - Solution.docx
55.5 KB
Quick reference
Operations: Apply Method
The Apply method allows us to write any python function we want and apply it to our dataFrame.
When to use
Use this any time you need to write python code to do complicated operations on a DataFrame.
Instructions
There are two main ways to use the apply method. The regular way, and using lambdas inline.
To use the regular way, simply write a python function, then apply it by wrapping the name of the function into the .apply() function:
pd.DataFrame(my_df["Mon"].apply(function_name))
To use lambdas, write your function code inside the .apply() function:
pd.DataFrame(my_df["Mon"].apply(lambda x: x * 10))
Hints & tips
- Regular Apply: pd.DataFrame(my_df["Mon"].apply(timer))
- Lambdas Apply: pd.DataFrame(my_df["Mon"].apply(lambda x: x * 10))
- 00:05 Okay, in this video we're going to look at the apply method.
- 00:07 And the apply method is super powerful and
- 00:10 you'll probably use this a lot in the future as you get more and
- 00:13 more advanced with data analysis, especially with Python.
- 00:18 Especially with pandas.
- 00:19 So the apply method allows us to create our own functions,
- 00:22 whatever functions we want, and apply them to the data in our columns.
- 00:28 So we learned a while back that we can do things to specific columns.
- 00:33 And we're just going to use the same data frame from our last video which is
- 00:35 right here.
- 00:36 So we can go my_df and then call, for instance, Monday.
- 00:42 And if we run this, we see we've just got this series of 1234,
- 00:45 which is what we see here 1234.
- 00:48 But imagine if we wanted to do something to say every value in this column,
- 00:54 let's say we wanted to multiply it by 10.
- 00:57 Each one of these, so this one will become 10, 20, 30 and then 40,
- 01:01 we could do that by just creating our own function.
- 01:03 So let's just create a quick function.
- 01:05 And let's call it anything you want.
- 01:06 I'm just going to call it times 10.
- 01:09 And we want to pass in x which is going to be the value of our column,
- 01:14 each individual value of the column.
- 01:18 So this is just a basic Python function.
- 01:20 And we could say return, let's say x times 10.
- 01:29 So now we can reference this
- 01:32 using the apply method just by calling our data frame.
- 01:35 So my_df.
- 01:37 And then we want to do this on let's say Monday, and
- 01:40 we just go .apply and call the function that we created earlier.
- 01:45 So that was times 10, if we do that, boom,
- 01:49 we get this series that has our column each value timesed by 10.
- 01:54 And we can make this into a data frame if we wanted to, we could go pd.data frame,
- 02:01 and pass that whole thing in and now we see Monday 10, 20, 30, 40.
- 02:07 If we wanted to take these times 100.
- 02:10 Now whatever we want to create a function to do, we can do,
- 02:15 we could turn these into a string and
- 02:20 then concatenate the word John onto each one.
- 02:25 If we ran this, I don't know why you would want to, one John, two John, three,
- 02:29 John, four John.
- 02:29 Anything you want to do, any function you want to create,
- 02:32 you can create and then use the apply function to apply it to your data frame.
- 02:36 So, very cool.
- 02:38 Very powerful, now this works, obviously, and it's very cool.
- 02:42 But sometimes you just want to create these on the fly.
- 02:45 And you can actually do that using lambdas.
- 02:47 If you're familiar with Python, you can create a basic lambda.
- 02:49 So I'm just going to comment this out just to
- 02:52 make sure we can prove that this has gone, it's no longer in our memory.
- 02:55 So now we can just call our data frame.
- 02:58 So my_df and again, let's say we want to do this on the Monday column.
- 03:05 And we still use the apply method, but instead of passing in, times 10 or
- 03:10 whatever, we could just create a lambda, we could go lambda, and then x.
- 03:19 X stands for each value of the column.
- 03:22 And then what do we want to do?
- 03:25 Let's say we want to times everything by 10, we would just go x times 10.
- 03:30 And boom, we get the same exact output.
- 03:33 And again, we could also pass this as a data frame so we could go pd.dataframe.
- 03:40 And there we get that again.
- 03:41 We wanted this to be 100, we'll mix 100, if we want to convert this to a string.
- 03:49 And then concatenate like we did earlier,
- 03:55 John, anything you want to do, you could just write it in line.
- 03:59 And a lot of times you just want to do simple things like
- 04:02 taking everything times 100 if you want to convert percentages or something.
- 04:06 Times everything by 100.
- 04:07 You'll just do this in line like this using a lambda
- 04:10 instead of creating an entire function and then calling the function.
- 04:15 It's just faster to do it like this.
- 04:17 And as you get sort of more advanced and more used to working with pandas,
- 04:20 it's nothing at all just to crank out a quick lambda like this, and
- 04:24 do whatever you want.
- 04:25 So that's the apply method, very powerful.
- 04:28 You can do all kinds of cool stuff with it.
- 04:30 And yeah, that's all there is to it.
- 04:33 So in the next video, we'll look at sorting.
Lesson notes are only available for subscribers.