Introduction to Using Apply/Map in Pandas
Explore how to effectively use Pandas apply, map, and applymap functions to perform complex operations on data. Understand applying custom functions with lambda expressions to categorize and analyze datasets like travel and medical records. This lesson helps you prepare for interview questions involving function applications across columns and rows in Pandas.
We'll cover the following...
Concept
So far you’ve been mainly using filters and basic manipulation through groups or dates. You’ve also covered basic aggregation concepts, such as getting the count of records, or the mean of a certain column.
Sometimes, you may need to apply more complex functions to each record, cell, or column.
Syntax
Assuming you have a certain function my_magic_fn to be applied to every value of a specific column, you can do the following:
df['my_col'].map(lambda x: my_magic_fn(x))
This applies your magic function to every value in my_col column.
Travel dataset
Idea
Different airlines have specific rules about which traveler is counted as a child, and how much of a discount they get. Applying these rules to all clients who request a trip can help in quickly identifying those who are eligible for discounts.
Interview
One possible question could be on how to categorize travelers into different categories based on certain rules. Some examples are as follows:
- If a traveler is solo and this is their second trip with your travel agency, you can categorize them as a loyal customer
- If a traveler is touring a historical city, you can categorize them as a culture and history enthusiast.
Medical dataset
Idea
Based on specific medical rules, new information can be derived about patients based on their symptoms.
Here is an example: patients who are diabetic and have high cholesterol levels can be categorized as high-risk patients. So, this rule can be applied and saved in a new risk column.
Interview
One possible question could be on how to apply a calculation involving multiple column values for different patients. For example, a more complex logic involving a patient’s pulse, oxygen saturation, and glucose levels could be derived.
Before you move to the challenges
- Make sure you know how to use the functions
.apply(), and.map(),.applymap() - Make sure you know how to use a
lambdafunction