Creating a Pivot Table
Taak
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.
Oplossing
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))
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 4. Hoofdstuk 6
single
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))
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.