Creating a Pivot Table
Tarea
Swipe to start coding
Your task here is to practice using pivot tables. Depending on the airline and the airport from which the flight started, calculate the number of delays and the minimum and maximum length of the flight. Follow the algorithm:
Create a pivot table:
- Assign the columns
'Airline'
and'AirportFrom'
to theindex
argument; - Assign the columns
'Delay'
and'Length'
to thevalues
argument (in this order); - Using the
aggfunc
argument, find the minimum and maximum of the'Length'
, and count the number of values in the'Delay'
column.
Solución
99
1
2
3
4
5
6
7
8
9
10
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/plane', index_col = 0)
# Create pivot table
data = pd.pivot_table(data, index = ['Airline', 'AirportFrom'],
values = ['Delay', 'Length'],
aggfunc = {'Length': ['min', 'max'], 'Delay': 'count'})
print(data.head(10))
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 4. Capítulo 6
99
1
2
3
4
5
6
7
8
9
10
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/plane', index_col = 0)
# Create pivot table
data = pd.pivot_table(data, index = ___,
values = [___],
aggfunc = {'___': [___, ___], '___': '___'})
print(data.head(10))
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla