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))
Lesson notes are only available for subscribers.