Looping and Aggregation

Learn about looping and aggregation methods of DataFrame.

We'll cover the following

Often we want to apply operations over items in a DataFrame. We may want to use looping, the apply method, or an aggregation method to do this.

For loops

We can use a for loop with a DataFrame, though we generally want to avoid for loops when doing numerical manipulation. Whenever there is a for loop with some pandas code, it means that it’s a slow operation, and we won’t be able to take advantage of the vectorization that speeds up many operations. However, sometimes a for loop is appropriate (for example, when labeling plots).

If we need to loop over a DataFrame, there are three methods for doing it:

  • The iteritems method gives us a tuple with the column name and the column (a Series).

Get hands-on with 1200+ tech skills courses.