Question: Here is my code and attached the error attached the error at the end, please advice to fix it . . . . . .

Here is my code and attached the error attached the error at the end, please advice to fix it............ import pandas as pd
# Step 1: Data Preparation
airports_df = pd.read_csv ('/Users/sureshvelusamy/Documents/Karthika/PY scripts/Airport_Codes.csv')
tickets_df = pd.read_csv ('/Users/sureshvelusamy/Documents/Karthika/PY scripts/Tickets.csv')
flights_df = pd.read_csv ('/Users/sureshvelusamy/Documents/Karthika/PY scripts/Flights.csv')
# Perform necessary data cleaning and filtering steps
tickets_df['ITIN_FARE']= tickets_df['ITIN_FARE'].str.replace('$','').astype('float64')
tickets_df['ITIN_FARE']= tickets_df['ITIN_FARE'].astype('Int64')
# Step 2: Identify the 10 busiest round trip routes
filtered_flights_df = flights_df[flights_df['CANCELLED']==0]
Roundtrip_Tickets= tickets_df[tickets_df['ROUNDTRIP']==1]
MLAirport=['medium_airport', 'large_airport']
round_trip_routes = filtered_flights_df.groupby(['ORIGIN', 'DESTINATION']).size().reset_index(name='Count')
top_10_busiest_routes = round_trip_routes.nlargest(10, 'Count')
# Step 3: Calculate the profitability of round trip routes
revenue_per_route = Roundtrip_Tickets.groupby(['ORIGIN', 'DESTINATION'])['ITIN_FARE'].sum().reset_index(name='TotalRevenue').nlargest(10, 'TotalRevenue')
revenue_per_route['ORIGIN']= pd.to_numeric(revenue_per_route['ORIGIN'], errors='coerce')
revenue_per_route['DESTINATION']= pd.to_numeric(revenue_per_route['DESTINATION'], errors='coerce')
revenue_per_route['TotalRevenue']= pd.to_numeric(revenue_per_route['TotalRevenue'], errors='coerce')
print(revenue_per_route.dtypes)
# Calculate the cost components for each route
cost_per_mile =8
cost_per_mile_other =1.18
airport_cost_medium =5000
airport_cost_large =10000
delay_cost_per_min =75
def calculate_route_cost(route):
origin, dest = route
distance = flights_df[(flights_df['ORIGIN']== origin) & (flights_df['DESTINATION']== dest)]['DISTANCE'].mean()
num_round_trips = round_trip_routes[(round_trip_routes['ORIGIN']== origin) & (round_trip_routes['DESTINATION']== dest)]['Count'].values[0]
airport_cost =(airports_df[airports_df['IATA_CODE']== origin]['TYPE'].values[0]== 'medium_airport')* airport_cost_medium +(airports_df[airports_df['IATA_CODE']== dest]['TYPE'].values[0]== 'medium_airport')* airport_cost_medium +(airports_df[airports_df['IATA_CODE']== origin]['TYPE'].values[0]== 'large_airport')* airport_cost_large +(airports_df[airports_df['IATA_CODE']== dest]['TYPE'].values[0]== 'large_airport')* airport_cost_large
operational_cost = distance * cost_per_mile + distance * cost_per_mile_other + airport_cost
delay_cost =(filtered_flights_df[(filtered_flights_df['ORIGIN']== origin) & (filtered_flights_df['DESTINATION']== dest)]['ARR_DELAY'].sum()+ filtered_flights_df[(filtered_flights_df['ORIGIN']== dest) & (filtered_flights_df['DESTINATION']== origin)]['DEP_DELAY'].sum())* delay_cost_per_min
total_cost = operational_cost + delay_cost
return total_cost * num_round_trips
cost_per_route = revenue_per_route.apply(lambda x: calculate_route_cost((x['ORIGIN'], x['DESTINATION'])), axis=1)
profit_per_route = revenue_per_route['TotalRevenue']- cost_per_route
top_10_profitable_routes = pd.DataFrame({
'Origin': top_10_busiest_routes['ORIGIN'],
'Dest': top_10_busiest_routes['DESTINATION'],
'TotalRevenue': revenue_per_route['TotalRevenue'],
'TotalCost': cost_per_route,
'Profit': profit_per_route
}).nlargest(10, 'Profit')
# Step 4: Recommend 5 round trip routes to invest in
recommended_routes = top_10_profitable_routes.nlargest(5, 'Profit')[['Origin', 'Dest']]
# Step 5: Calculate the breakeven point for upfront airplane costs
upfront_airplane_cost =90000000 # $90 million
breakeven_flights =[]
for route in recommended_routes.itertuples(index=False):
origin, dest = route
revenue = revenue_per_route[(revenue_per_route['ORIGIN']== origin) & (revenue_per_route['DESTINATION']== dest)]['TotalRevenue'].values[0]
cost = cost_per_route[(revenue_per_route['ORIGIN']== origin) & (revenue_per_route['DESTINATION']== dest)]
breakeven_point = upfront_airplane_cost /(revenue - cost)
breakeven_flights.append(breakeven_point)
# Step 6: Recommend Key Performance Indicators (KPIs)
recommended_kpis =['Load Factor', 'On-time Performance', 'Customer Satisfaction']
# Print the results
print("10 Busiest Round Trip Routes:")
print(top_10_busiest_routes)
print("
10 Most Profitable Round Trip Routes:")
print(top_10_profitable_routes)
print("
Recommended 5 Round Trip Routes:")..................Error .............................. IndexError Traceback (most recent call last) Cell In[174], line 85--->85 cost_per_route = revenue_per_route.apply(lambda x: calculate_route_cost((x['ORIGIN'], x[

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!