Course Content

## Group by Several Columns

Let's add some information on the `.groupby()` function. You can group by several columns, but the order is crucial in this case. In the previous chapter, we grouped data by the flight number and counted the number of delays. We can make this task complicated by grouping not only by the `'Flight'` column, but also by the column `'Airline'`. Refresh the information on the dataset and then look at this simple example (the output contains only the first 10 rows):

Explanation:

• `data[['Flight', 'Delay', 'Airline']]` - columns you will work with, including the columns by which you will group.
• `.groupby(['Flight', 'Airline'])` - here, `'Flight'` and `'Airline'` are arguments of the function `.groupby()`.
Pay attention; if you want to group by several columns, put them into the list - the order is crucial. So, in our case, if rows of the data set have the same value in the column `'Flight'`, they will relate to one group. Then inside those groups, the function finds other groups for rows with the same value in the column `'Airline'`. Then, due to the function `.count()` that counts the rows, our function will calculate the number of rows in the column `'Delay'` that have the same value in the column `'Airline'` for each `'Flight'` group.

• Extract the columns `'AirportFrom'`, `'DayOfWeek'`, and `'Time'` from `data` (in this order).
• Apply the `.groupby()` function to the previous columns.
• Within the `.groupby()` function, put the columns `'AirportFrom'` and `'DayOfWeek'`; the order is crucial.
• Calculate the mean value of the column `'Time'`.
2. Output the first `10` rows of the `data_flights`.