Dealing With Pivot Tables | Aggregating Data

# Dealing With Pivot Tables

Python has an analog of the `.groupby()` function that can lead to the same result. It is up to you as to which function to use. Let's learn it by using an example. Using the following function, called `.pivot_table()`, we will calculate the mean values of the column `'Length'` that have the same value in the column `'Flight'`:

Explanation:

• `pd.pivot_table()` - function that creates pivot tables;
• `data` - data frame that we use;
• `values = 'Length'` - to the argument `values`, we assign columns having the same group, for which we will apply the calculation of the average, maximum, etc. If you want to group by several columns, put them in the list; the order isn't crucial;
• `index = 'Flight'` - `index` is an argument to which you assign the name of a column or columns that you want to group. If you want to group by several columns, put them in the list; the order is crucial, like in the `.groupby()` function;
• `aggfunc = 'mean'` - the same as `agg` in the `.groupby()` function, `aggfunc` has exactly the same syntax as `agg`. Thus, you can put several functions here by putting them in the list to specify functions for different columns using curly brackets.

Here, you have the example of a code that uses the `.groupby()` statement. Please choose the analog using the `.pivot_table()` function.

Everything was clear?

Section 4. Chapter 5

Course Content

# Dealing With Pivot Tables

Python has an analog of the `.groupby()` function that can lead to the same result. It is up to you as to which function to use. Let's learn it by using an example. Using the following function, called `.pivot_table()`, we will calculate the mean values of the column `'Length'` that have the same value in the column `'Flight'`:

Explanation:

• `pd.pivot_table()` - function that creates pivot tables;
• `data` - data frame that we use;
• `values = 'Length'` - to the argument `values`, we assign columns having the same group, for which we will apply the calculation of the average, maximum, etc. If you want to group by several columns, put them in the list; the order isn't crucial;
• `index = 'Flight'` - `index` is an argument to which you assign the name of a column or columns that you want to group. If you want to group by several columns, put them in the list; the order is crucial, like in the `.groupby()` function;
• `aggfunc = 'mean'` - the same as `agg` in the `.groupby()` function, `aggfunc` has exactly the same syntax as `agg`. Thus, you can put several functions here by putting them in the list to specify functions for different columns using curly brackets.

Here, you have the example of a code that uses the `.groupby()` statement. Please choose the analog using the `.pivot_table()` function.