-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
extra/pandas/the-airport-with-the-most-traffic/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pandas as pd | ||
|
||
|
||
def airport_with_most_traffic(flights: pd.DataFrame) -> pd.DataFrame: | ||
return ( | ||
pd.concat( | ||
[ | ||
flights.rename(columns={"departure_airport": "airport_id"}), | ||
flights.rename(columns={"arrival_airport": "airport_id"}), | ||
] | ||
) | ||
.groupby("airport_id") | ||
.flights_count.sum() | ||
.reset_index(name="total") | ||
.query("total == total.max()")[["airport_id"]] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
WITH Airports AS ( | ||
SELECT departure_airport AS airport_id, | ||
flights_count | ||
FROM Flights | ||
UNION ALL | ||
SELECT arrival_airport AS airport_id, | ||
flights_count | ||
FROM Flights | ||
), RankedAirports AS ( | ||
SELECT airport_id, | ||
RANK() OVER(ORDER BY SUM(flights_count) DESC) AS rnk | ||
FROM Airports | ||
GROUP BY airport_id | ||
) | ||
SELECT airport_id | ||
FROM RankedAirports | ||
WHERE rnk = 1; |