In: Computer Science
{PYTHON }You have a CSV file containing the location and population of various cities around the world. For this question you'll be given a list of cities and return the total population across all those cities. Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a list containing 3 strings as elements representing the CountryCode, CityName, and Region in this order. Return the total population of all cities in the list. Note that the city must match the country, name, and region to ensure that the correct city is being read.
import pandas as pd #to read csv into a dataset
def total_population(file_name, city_list): #function definition
with required parameters
data = pd.read_csv(file_name) #reading a csv file
pop = 0 #for storing population
for i in city_list: #looping through the list
temp = data.loc[(data.Country == int(i[0])) & (data.City ==
i[1]) & (data.Region == i[2])] #selecting row matching the
conditions
pop += int(temp['Population']) #summing population for each
city
return pop
error on input ['cities.csv', [['nl', 'vlissingen', '10'], ['ca', 'whitehorse', '12'], ['ph', 'camflora', 'H2'], ['gb', 'johnshaven', 'T6']]]: 'DataFrame' object has no attribute 'CountryCode'
also that return outside function
Here is the code:
def total_population(filename,city_list):
data = pd.read_csv(filename) # reading the data
all_population = list() # creating a list to store the corresponding populations
for city in city_list:
for loop1 in range(len(data)):
if(city[0] == data['CountryCode'][loop1] and city[1] == data['CityName'][loop1] and city[2] == data['Region'][loop1]):
all_population.append(data['Population'][loop1])
# printing the results
for loop2 in range(len(city_list)):
print('Population of ' + str(city_list[loop2][1]) + ' is: ' + str(all_population[loop2]))
# calling the function
city_list = [[33, 'Chicago', 'West'],[22,'DC','East']]
total_population('sample_data_df.csv',city_list)
Here is the results:
For any doubt please comment below.