Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 295 additions & 0 deletions [lab-working-with-api] Sara.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4bcf2922",
"metadata": {},
"source": [
"##### Create a function that returns the price, names of origin and arrival airports and the name of the company. Do it for all the flights between two dates that cost the same."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f678869d",
"metadata": {},
"outputs": [],
"source": [
"data = {'Quotes': [{'QuoteId': 1,\n",
" 'MinPrice': 92.0,\n",
" 'Direct': False,\n",
" 'OutboundLeg': {'CarrierIds': [1065],\n",
" 'OriginId': 81727,\n",
" 'DestinationId': 50290,\n",
" 'DepartureDate': '2020-12-12T00:00:00'},\n",
" 'QuoteDateTime': '2020-09-21T10:49:00'}, \n",
" {'QuoteId': 2,\n",
" 'MinPrice': 133.0,\n",
" 'Direct': True,\n",
" 'OutboundLeg': {'CarrierIds': [851],\n",
" 'OriginId': 81727,\n",
" 'DestinationId': 50290,\n",
" 'DepartureDate': '2020-12-12T00:00:00'},\n",
" 'QuoteDateTime': '2020-09-21T10:49:00'}],\n",
" \n",
" 'Places': [{'PlaceId': 50290,\n",
" 'IataCode': 'EWR',\n",
" 'Name': 'New York Newark',\n",
" 'Type': 'Station',\n",
" 'SkyscannerCode': 'EWR',\n",
" 'CityName': 'New York',\n",
" 'CityId': 'NYCA',\n",
" 'CountryName': 'United States'},\n",
" {'PlaceId': 60987,\n",
" 'IataCode': 'JFK',\n",
" 'Name': 'New York John F. Kennedy',\n",
" 'Type': 'Station',\n",
" 'SkyscannerCode': 'JFK',\n",
" 'CityName': 'New York',\n",
" 'CityId': 'NYCA',\n",
" 'CountryName': 'United States'},\n",
" {'PlaceId': 65633,\n",
" 'IataCode': 'LGA',\n",
" 'Name': 'New York LaGuardia',\n",
" 'Type': 'Station',\n",
" 'SkyscannerCode': 'LGA',\n",
" 'CityName': 'New York',\n",
" 'CityId': 'NYCA',\n",
" 'CountryName': 'United States'},\n",
" {'PlaceId': 81727,\n",
" 'IataCode': 'SFO',\n",
" 'Name': 'San Francisco International',\n",
" 'Type': 'Station',\n",
" 'SkyscannerCode': 'SFO',\n",
" 'CityName': 'San Francisco',\n",
" 'CityId': 'SFOA',\n",
" 'CountryName': 'United States'}],\n",
" \n",
" 'Carriers': [{'CarrierId': 819, 'Name': 'Aegean Airlines'},\n",
" {'CarrierId': 851, 'Name': 'Alaska Airlines'},\n",
" {'CarrierId': 870, 'Name': 'jetBlue'},\n",
" {'CarrierId': 1065, 'Name': 'Frontier Airlines'},\n",
" {'CarrierId': 1721, 'Name': 'Sun Country Airlines'},\n",
" {'CarrierId': 1793, 'Name': 'United'},\n",
" {'CarrierId': 1902, 'Name': 'Southwest Airlines'}],\n",
" \n",
" 'Currencies': [{'Code': 'USD',\n",
" 'Symbol': '$',\n",
" 'ThousandsSeparator': ',',\n",
" 'DecimalSeparator': '.',\n",
" 'SymbolOnLeft': True,\n",
" 'SpaceBetweenAmountAndSymbol': False,\n",
" 'RoundingCoefficient': 0,\n",
" 'DecimalDigits': 2}]}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "61b61e11",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0755b6b0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'QuoteId': 1,\n",
" 'MinPrice': 92.0,\n",
" 'Direct': False,\n",
" 'OutboundLeg': {'CarrierIds': [1065],\n",
" 'OriginId': 81727,\n",
" 'DestinationId': 50290,\n",
" 'DepartureDate': '2020-12-12T00:00:00'},\n",
" 'QuoteDateTime': '2020-09-21T10:49:00'},\n",
" {'QuoteId': 2,\n",
" 'MinPrice': 133.0,\n",
" 'Direct': True,\n",
" 'OutboundLeg': {'CarrierIds': [851],\n",
" 'OriginId': 81727,\n",
" 'DestinationId': 50290,\n",
" 'DepartureDate': '2020-12-12T00:00:00'},\n",
" 'QuoteDateTime': '2020-09-21T10:49:00'}]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data['Quotes']"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7df9823f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'QuoteId': 1,\n",
" 'MinPrice': 92.0,\n",
" 'Direct': False,\n",
" 'OutboundLeg': {'CarrierIds': [1065],\n",
" 'OriginId': 81727,\n",
" 'DestinationId': 50290,\n",
" 'DepartureDate': '2020-12-12T00:00:00'},\n",
" 'QuoteDateTime': '2020-09-21T10:49:00'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data['Quotes'][0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "db8425ec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"92.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data['Quotes'][0]['MinPrice']"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "0f80aa19",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: tabulate in c:\\users\\ssara\\anaconda3\\lib\\site-packages (0.8.10)Note: you may need to restart the kernel to use updated packages.\n",
"\n"
]
}
],
"source": [
"pip install tabulate"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "fb7ed419",
"metadata": {},
"outputs": [],
"source": [
"from tabulate import tabulate\n",
"\n",
"# Building the Function\n",
"def flight_info():\n",
" flights_list = []\n",
" \n",
" for flight in range(2):\n",
" price = data[\"Quotes\"][flight][\"MinPrice\"]\n",
" origin = data[\"Quotes\"][flight]['OutboundLeg'][\"OriginId\"]\n",
"\n",
" for place_dict in data[\"Places\"]:\n",
" if place_dict[\"PlaceId\"] == origin:\n",
" origin_name = place_dict[\"Name\"]\n",
" \n",
" destination = data[\"Quotes\"][flight]['OutboundLeg'][\"DestinationId\"]\n",
"\n",
" for place_dict in data[\"Places\"]:\n",
" if place_dict[\"PlaceId\"] == destination:\n",
" destination_name = place_dict[\"Name\"]\n",
"\n",
" company = data[\"Quotes\"][flight][\"OutboundLeg\"][\"CarrierIds\"][0]\n",
"\n",
" for company_dict in data[\"Carriers\"]:\n",
" if company_dict[\"CarrierId\"] == company:\n",
" company_name = company_dict[\"Name\"]\n",
"\n",
" info_dict = {\n",
" \"Price\": price,\n",
" \"Origin airport\": origin_name,\n",
" \"Arrival airport\": destination_name, \n",
" \"Company\": company_name\n",
" }\n",
" \n",
" flights_list.append(info_dict)\n",
" \n",
" return flights_list"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "f4b27c8a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Price Origin airport Arrival airport Company\n",
"------- --------------------------- ----------------- -----------------\n",
" 92 San Francisco International New York Newark Frontier Airlines\n",
" 133 San Francisco International New York Newark Alaska Airlines\n"
]
}
],
"source": [
"# Calling the function and store the result\n",
"flight_data = flight_info()\n",
"\n",
"# Displaying flight information in a table\n",
"table_headers = flight_data[0].keys()\n",
"table_rows = [list(flight.values()) for flight in flight_data]\n",
"\n",
"print(tabulate(table_rows, headers=table_headers))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}