Python's Pandas Series/Dataframe Questions (If Statements and Loops are NOT ALLOWED)
Consider the distances.txt (pasted below) file. Such a file can be read into a pandas DataFrame arranged like so:
Chicago | Los Angeles | New York | Philadephia | |
Chicago | 0 | 2054 | 802 | 738 |
Los Angeles | 2054 | 0 | 2786 | 2706 |
New York | 802 | 2786 | 0 | 100 |
Philadephia | 738 | 2706 | 100 | 0 |
Write a function named total_distance that accepts a distance matrix (like the one above) and an iterable list of cities. Your function should then calculate the total distance traveled when visiting the cities in that order and returning to the starting city. As with the other exercises, your function should use only vectorized pandas operations and no loops or list comprehensions. Here are some examples:
In [1]: total_distance(dist_matrix,['Chicago','Los Angeles'])
Out[1]: 4108
In [2]: total_distance(dist_matrix,['Chicago','Los
Angeles','Philadelphia'])
Out[2]: 5498
In [3]: total_distance(dist_matrix,['Chicago','Los
Angeles','Philadelphia','New York'])
Out[3]: 5662
In [4]:total_distance(dist_matrix,['Chicago','Los
Angeles','Philadelphia','New York','Los Angeles'])
Out[4]: 9700
The file distances.txt is pasted below:
0,2054,802,738
2054,0,2786,2706
802,2786,0,100
738,2706,100,0.
This problem is often known as calculating a "tour". If you want to have some fun, you can then use your function to solve a famous problem. Can you name it? Here's some sample code:
import pandas as pd
import itertools
ci t i e s = [ ' Chicago ' , ' Los Angeles ' , 'New York ' , '
Philadelphia ' ]
dist_matrix = pd . read_csv ( ' d i s t a n c e s . txt ' ,
header=None , names=cities )
di s t_mat r ix . index = c i t i e s
shor t e s t_tour = [ ]
sh o r t e s t_d i s t = 1000000
for a_tour in i t e r t o o l s . permutat ions ( c i t i e s , 4 )
:
di s t anc e = t o t a l_di s t anc e ( di s t_mat r ix , l i s t (
a_tour ) )
i f di s t anc e < sh o r t e s t_d i s t :
sh o r t e s t_d i s t = di s t anc e
shor t e s t_tour = a_tour
print ( sho r t e s t_di s t , shor t e s t_tour )
Point of Stress: This is a Python question requiring the use of Pandas Series or DataFrame (please loops are not allowed - zero score for using loops). Please attach a print screen in addition to the codes to show the scripts ran properly and as intended.
Answer: See the code for function below:
------------------------------------------
import pandas as pd
cities = ['Chicago','Los Angeles','New York','Philadelphia']
dist_matrix =
pd.read_csv('distances.txt',header=None,names=cities)
dist_matrix.index = cities
#function to calculate total distance for a given sequence of
cities
def total_distance(dist_matrix,cities_list):
#origin cities
origins = cities_list
#print(origins)
#destination cities
dests = cities_list[1:len(cities_list)]
dests.append(cities_list[0])
#print(dests)
#return distance covered
return
sum(dist_matrix.lookup(origins,dests))
tour=['Chicago','Los Angeles']
print("tour:",tour)
print("distance covered:",total_distance(dist_matrix,tour))
tour=['Chicago','Los Angeles','Philadelphia']
print("tour:",tour)
print("distance covered:",total_distance(dist_matrix,tour))
tour=['Chicago','Los Angeles','Philadelphia','New
York']
print("tour:",tour)
print("distance covered:",total_distance(dist_matrix,tour))
tour=['Chicago','Los Angeles','Philadelphia','New York','Los
Angeles']
print("tour:",tour)
print("distance covered:",total_distance(dist_matrix,tour))
--------------------------------------------------------------------------------------
Output:
Python's Pandas Series/Dataframe Questions (If Statements and Loops are NOT ALLOWED) Consider the distances.txt (pasted below)...
Western Airlines wants to design a hub system in the United States. Each hub is used for connecting flights to and from cities within 1500 miles of the hub. Western runs flights among the following cities: Atlanta, Boston, Chicago, Denver, Houston, Los Angeles, New Orleans, New York, Pittsburgh, Salt Lake City, San Francisco, and Seattle. The company wants to determine the smallest number of hubs it needs to cover all these cities. A city is covered if it is within...
Use the following information to answer questions 25-31 A traveler wants to know if the prices of hotels are different. She samples 10 cities and finds the prices below. Use a paired-sample t-test to determine whether the difference between hotel prices is significant at the a 0.01 confidence level Cities Atlanta Boston Chicago Dallas Denver Indianapolis Los Angeles New York City 517 Philadelphia Washington, DC 251 Hyatt Regency prices in dollars Hilton prices in dollars 90 273 204 303 189...
It is based on the multiple-choice question pasted below. Use the current 21 percent tax rate. (28) in the current year, Acom, Inc., had the following items of income and expense! Sales $500,000 Cost of sales 250,000 Dividends received 25,000 The dividends were received from a corporation of which Acom owns 30%. In Acom's current yoar income tax rotum, what amount should be reported as income before special deductions? A. $525.000 B. $508,750 C. $275,000 D. $250.000 The correct answer...
Write MySQL query statements for the questions below including the output that proves the accuracy of your solution. Your answers should be stored in a text file that captures your interaction with MySQL. 1. Find the movieID, title, year and DVDPrice of all movies where the DVD-Price is equal to the discountPrice. 2. Find the actorID, lastName, firstName, middleName, and suffix of all actors whose middleName is not NULL. 3. Suppose you remember a movie quote as “Play it again,...