Counting the total number of flights between cities
In the flights dataset, we have data on the origin and destination airport. It is trivial to count the number of flights originating in Houston and landing in Atlanta, for instance. What is more difficult is counting the total number of flights between the two cities.
In this recipe, we count the total number of flights between two cities, regardless of which one is the origin or destination. To accomplish this, we sort the origin and destination airports alphabetically so that each combination of airports always occurs in the same order. We can then use this new column arrangement to form groups and then to count.
How to do it…
- Read in the flights dataset, and find the total number of flights between each origin and destination airport:
>>> flights = pd.read_csv('data/flights.csv') >>> flights_ct = flights.groupby(['ORG_AIR', 'DEST_AIR']).size...