Question

In: Computer Science

PYTHON - You are given a data.csv file in the /root/customers/ directory containing information about your...

PYTHON - You are given a data.csv file in the /root/customers/ directory containing information about your customers. It has the following columns: ID,NAME,CITY,COUNTRY,CPERSON,EMPLCNT,CONTRCNT,CONTRCOST where ID: Unique id of the customer NAME: Official customer company name CITY: Location city name COUNTRY: Location country name CPERSON: Email of the customer company contact person EMPLCNT: Customer company employees number CONTRCNT: Number of contracts signed with the customer CONTRCOST: Total amount of money paid by customer (float in format dollars.cents) Read and analyze the data.csv file, and output the answers to these questions: How many total customers are in this data set? How many customers are in each city? How many customers are in each country? Which country has the largest number of customers' contracts signed in it? How many contracts does it have? How many unique cities have at least one customer in them? The answers should be formatted as: Total customers: Customers by city: : : ... Customers by country: : : ... Country with most customers' contracts: USA ( contracts) Unique cities with at least one customer: The answers for Customers by city and Customers by country must be sorted by CITY and COUNTRY respectively, in ascending order. If there are several cities that are tied for having the most customers' contracts, print the lexicographically bigger one.

Solutions

Expert Solution

We are using the built-in csv module to read in the data.csv file. The program code, as per the given requirements, is as follows:

import csv

def analyze_data():
    filename = "/root/customers/data.csv"
    rows = []
    city_cust_dict = {} # Dictionary with key as the city, and value as number of customers in that city
    country_cust_dict = {} # Dictionary with key as the country, and value as number of customers in that country

    with open(filename, 'r') as file:
        reader = csv.reader(file)
        fields = next(reader)
        for row in reader:
            rows.append(row)
            city = row[2]
            country = row[3]

            # Add city, customer to city_cust_dict dictionary
            if city in city_cust_dict:
                city_cust_dict[city] += 1
            else:
                city_cust_dict[city] = 1

            # Add country, customer to country_cust_dict dictionary
            if country in country_cust_dict:
                country_cust_dict[country] += 1
            else:
                country_cust_dict[country] = 1


    # *** PRINTING REQUIRED DATA ***

    # How many total customers are in this data set?
    print("Total customers: " + str(len(rows)))

    # How many customers are in each city?
    sorted(city_cust_dict.items(), key=lambda x: x[1])
    print("Customers by city:")
    for k, v in city_cust_dict.items():
        print(k + " : " + str(v))

    # How many customers are in each country?
    sorted(country_cust_dict.items(), key=lambda x: x[1])
    print("Customers by country:")
    for k, v in country_cust_dict.items():
        print(k + " : " + str(v))

    # Country with most customers' contracts?
    country_keys = list(country_cust_dict.keys())
    max_country = country_keys[0]
    max_val = country_cust_dict[max_country]
    for i in range(len(country_keys)):
        k = country_keys[i]
        v = country_cust_dict[k]
        if v > max_val:
            max_val = v
            max_country = k
        elif v == max_val:
            if k > max_country:
                k = max_country
    max_val = country_cust_dict[max_country]
    print("Country with most customers' contracts: " + max_country + " (" + str(max_val) + ")")

    # How many unique cities have at least one customer in them?
    print("Unique cities with at least one customer:")
    for x in city_cust_dict.keys():
        print(x)


if __name__ == '__main__':
    analyze_data()

Hope that helps!


Related Solutions

in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Write a program using python that loops over each file in a specified directory and checks...
Write a program using python that loops over each file in a specified directory and checks the size of each file.You should create 2-tuple with the filename and size, should append the 2-tuple to a list, and then store all the lists in a dictionary.  
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{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...
{PYTHON }You have a CSV file containing the location and population of various cities around the...
{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...
You first needs to create a file called “myJS.js” in your project directory. Then, include the...
You first needs to create a file called “myJS.js” in your project directory. Then, include the AngularJS library and myJS.js in the header of the index.html page. Now, you need to define your angular application and a controller in this file such that the controller has the following variables with these initial values: name = "Alex Cool"; faculty= "Faculty of Busienss and IT"; university = {name:"University of Ontario Institute of Technology", url:"http://www.uoit.ca"}; contacts = {email:"[email protected]", phone:"xxx-xxx-xxxx"}; skills = [ {name:...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
In this assignment you will be given an input file containing a series of universities along...
In this assignment you will be given an input file containing a series of universities along with that universities' location and rating. You must write a program that uses user defined functions to output a list of all school's above a certain rating to the terminal. Your program should do the following: - Prompt the user for the name of the input file to open - Prompt the user for the rating threshold ( Note: we print if the rating...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT