The Globalization and World Cities Research Network (GaWC) is a think tank that studies the relationships between world cities in the context of globalization.
These data were collected in 2000. They are the service values (indicating the importance of a city in the office network of a firm) of 100 global service firms distributed across 315 cities worldwide. All firms supply advanced producer services (accountancy, advertising, banking/finance, insurance, law, and management consultancy) through offices in at least 15 cities (including at least one in each of Pacific Asia, western Europe and northern America).
John Hancock Center, Chicago 2018 | Photograph by Rohan Dawar
The Raw Data:
GaWC provides Comma Separated Value (CSV) data with the list of 315 cities in the first column, and each subsequent column containing the aforementioned global service firms and their rankings (1-5) indicating the importance of that city for the firm:
The End Goal:
The goal of this script is to input the above csv and output a list of the cities sorted by the sum of their scores.
Step 1: CSV -> Python Lists
The first step involves importing the csv library and writing a function that separates the csv data into a list of lists where the sublists include the city name in the 0th index and the firm scores as the subsequent list items:
import csv
csv_name = 'da11.csv'
def sep(f):
global name, cities
name = [] #names of all the service firms
cities = [] #city name and their scores
with open(csv_name, newline='') as f:
csv_reader = csv.reader(f)
name = next(csv_reader)
for row in csv_reader:
cities.append(row)
Step 2: Separate City Names From Their Scores
First, a function 'sepcity' which returns a list of city names:
def sepcity():
listofcities = []
for city in cities:
cityname = city[0]
listofcities.append(cityname)
return listofcities
Next, a function 'sepstat' which returns a single number representing the sum of firm scores for that city:
def sepstat():
sumofpoints = []
for i in cities:
remain = i[1:]
sumofpoints.append(sum(map(int, remain)))
return sumofpoints
Step 3: Create A Sorted Dictionary of Key-Value Pairs
In our case the 'keys' are the names of cities and their values are the sum of their firm scores. To sort dictionaries we can use the collections library.
import collections
def key(lCities,lScores):
pairs = {}
for i in range(len(cities)):
pairs[lCities[i]] = lScores[i]
return pairs
def sort_key(pairs):
sorted_pairs = sorted(pairs.items(), key=lambda kv: kv[1])
sorted_dict = dict(collections.OrderedDict(reversed(list(sorted_pairs))))
return sorted_dict
Step 4: Create A Ranklist File
Now that we have the ordered dictionary, we have reached our goal. But for convenience sake, we can write the dictionary to a txt file so the user can open it in their notepad. We simply need to write it to a file with some basic formatting and open it using the os library:
import os
def create_ranklist(d):
f = 'rankings.txt'
textfile = open(f,'w')
d = str(d)
a = len(d)
d = d[1:a-1]
d = f'1 {d}'
r = 2
for i in d:
if i != ",":
textfile.write(i)
else:
textfile.write(f'\n{str(r)})
r += 1
os.startfile(f)
Step 5: Run
Now we nest all our functions feeding into the next like dominoes:
sep(csv_name)
create_ranklist(sort_key(key(sepcity(),sepstat()))
Results:
Script File Download Link:
Raw Data Source: https://www.lboro.ac.uk/gawc/datasets/da11.html
Rankings.txt Download Link:
Comments