# Group

Let's expand our knowledge on the `.groupby()`

function. As you remember, we can use the `.agg()`

function. Indeed, the main pros of this function are that we can apply a different function to the numerical columns with one group key. Look at the example where we grouped flights by the column `'Airline'`

, then counted the values in `'Delay'`

for each `'Airline'`

, and calculated the minimum and maximum values for the `'Length'`

column. So convenient, isn't it?

**Explanation:**

`.agg()`

- a function that allows us to apply functions on a series or on each element separately;`{}`

- we use curly brackets to specify the column and apply functions to them directly;`'Delay': 'count'`

- applies the`.count()`

function to the values in the`'Delay'`

column having the same key group;`'Length': ['min', 'max']`

- applies the`.min()`

and`.max()`

functions to the values in the`'Length'`

column having the same key group. You just need to put just the column name without the`()`

or`.`

symbols in the function. Pay attention; if you want to apply several functions to the same column, you must put them into the list.

Task

We can assume that a delay depends on the airline or the airport, but let's dive deeper and look at the average and maximum delay times depending on the airport from which the flight started and then on the airport at which the flight ended. Also, look at the median length of the flight. Follow the algorithm:

Group data:

- Apply the
`.groupby()`

function to the dataset`data`

; - Within the
`.groupby()`

function, put the columns`'AirportFrom'`

and`'AirportTo'`

; the order is crucial; - Using the
`.agg()`

function, calculate the aggregated values: the average and maximum value in the column`'Time'`

, and the median value of the column`'Length'`

.

Everything was clear?

Course Content

Advanced Techniques in pandas

## Advanced Techniques in pandas

1. Get Familiar With Indexing and Selecting Data

# Group

Let's expand our knowledge on the `.groupby()`

function. As you remember, we can use the `.agg()`

function. Indeed, the main pros of this function are that we can apply a different function to the numerical columns with one group key. Look at the example where we grouped flights by the column `'Airline'`

, then counted the values in `'Delay'`

for each `'Airline'`

, and calculated the minimum and maximum values for the `'Length'`

column. So convenient, isn't it?

**Explanation:**

`.agg()`

- a function that allows us to apply functions on a series or on each element separately;`{}`

- we use curly brackets to specify the column and apply functions to them directly;`'Delay': 'count'`

- applies the`.count()`

function to the values in the`'Delay'`

column having the same key group;`'Length': ['min', 'max']`

- applies the`.min()`

and`.max()`

functions to the values in the`'Length'`

column having the same key group. You just need to put just the column name without the`()`

or`.`

symbols in the function. Pay attention; if you want to apply several functions to the same column, you must put them into the list.

Task

We can assume that a delay depends on the airline or the airport, but let's dive deeper and look at the average and maximum delay times depending on the airport from which the flight started and then on the airport at which the flight ended. Also, look at the median length of the flight. Follow the algorithm:

Group data:

- Apply the
`.groupby()`

function to the dataset`data`

; - Within the
`.groupby()`

function, put the columns`'AirportFrom'`

and`'AirportTo'`

; the order is crucial; - Using the
`.agg()`

function, calculate the aggregated values: the average and maximum value in the column`'Time'`

, and the median value of the column`'Length'`

.

Everything was clear?