Introduction to Using Apply/Map in Pandas

This section introduces the concept of applying functions to a Pandas DataFrame using apply/map.

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.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy