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.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.