Pandas apply and map

The way to apply a function to pandas data structures is not always obvious–several methods exist (apply, applymap, map) and their scope is different.

First there is two main structures (fortunately I’m not talking about Panel here):

The apply / map methods can work on different ways.

Element-wise

The function is called (mapped) for each individual element (value)–so it takes the element (each distinct value) as parameter.

By row / column

The function is called (applied) for an entire row or a column–so it takes a row or a column as parameter, in other words a Series.

In short, apply works on row / column of a DataFrame, applymap works element-wise on a DataFrame, and map–and apply for most cases–works element-wise on a Series.

References / Further reading

  1. Wes McKinney, Python for Data Analysis ( O’Reilly, 2012)
  2. Difference between map, applymap and apply methods in Pandas