CSE-8A / PA3 / 2nd test PA 3.py
2nd test PA 3.py
Raw
#list1 = ["Males", "Females", "Males", "Males", "Males", "Females", "Females"]
#list2 = list1
#num_males = len(list1) # shows the number of stuff in list1


#num_males = list1.count("Males")
# BEGIN PROVIDED CODE
# ingest_data: given a file name and a field name, returns a list containing
# all the data in the given file for the given field name
import csv
def ingest_data(filename, fieldname):
    file_object = open(filename, newline='')
    rows = csv.reader(file_object, delimiter=',')
    headers = next(rows)
    try:
        field_idx = headers.index(fieldname)
    except ValueError:
        print('The field name', fieldname, 'does not exist in the headers.')
        print('Here are the value field names in this file:')
        for h in headers:
            print(h)
        return
    data_list = []
    count = 0
    limit = 2000 # CHANGE LIMIT
    for line in rows:
        if (count >= limit):
            print('Too many entries, returning first', limit, 'entries.')
            return data_list
        try:
            field_value = line[field_idx]
        except IndexError:
            print('Skipping row #', count, 'because field does not exist')
            continue
        data_list.append(field_value)
        count = count + 1
    return data_list

Gender = ingest_data("data.csv", "Sex")
Location = ingest_data("data.csv", "Location")

list1 = Gender
list2 = Location

#the locations and then number of people that visited it
num_hospital = Location.count("Hospital")
num_residence = Location.count("Residence")
num_other = Location.count("Other")
num_location_unknown = 2000- (num_hospital + num_residence + num_other)

num_males = Gender.count('Male')
num_females = Gender.count('Female')
num_gender_unknown = 2000-(num_males + num_females)
i= 0
#list1 = ["Males", "Females", "Males", "Males", "Males", "Females", "Females"]
#list2 = list1
#num_males = len(list1) # shows the number of stuff in list1


#num_males = list1.count("Males")
# BEGIN PROVIDED CODE
# ingest_data: given a file name and a field name, returns a list containing
# all the data in the given file for the given field name
import csv
def ingest_data(filename, fieldname):
    file_object = open(filename, newline='')
    rows = csv.reader(file_object, delimiter=',')
    headers = next(rows)
    try:
        field_idx = headers.index(fieldname)
    except ValueError:
        print('The field name', fieldname, 'does not exist in the headers.')
        print('Here are the value field names in this file:')
        for h in headers:
            print(h)
        return
    data_list = []
    count = 0
    limit = 2000 # CHANGE LIMIT
    for line in rows:
        if (count >= limit):
            print('Too many entries, returning first', limit, 'entries.')
            return data_list
        try:
            field_value = line[field_idx]
        except IndexError:
            print('Skipping row #', count, 'because field does not exist')
            continue
        data_list.append(field_value)
        count = count + 1
    return data_list
#Ingest data code ends here
Gender = ingest_data("data.csv", "Sex")
Location = ingest_data("data.csv", "Location")

list1 = Gender
list2 = Location

#the locations and then number of people that visited it
num_hospital = Location.count("Hospital")
num_residence = Location.count("Residence")
num_other = Location.count("Other")
num_location_unknown = 2000- (num_hospital + num_residence + num_other)

num_males = Gender.count('Male')
num_females = Gender.count('Female')
num_gender_unknown = 2000-(num_males + num_females)
i= 0

#Male = Gender.index('Male')
#Hospital = Location.index('Hospital')
Total_num_males_hospital = 0
Total_num_males_residence = 0
Total_num_males_other = 0
Total_num_males_unknown = 0
Total_num_females_hospital = 0
Total_num_females_residence = 0
Total_num_females_other = 0
Total_num_females_unknown = 0
Total_num_unknown = 0

for i in range(2000):
    if Gender[i] == ('Male') and Location[i] == ('Hospital'):
        Total_num_males_hospital += 1
        
    elif Gender[i] ==('Male') and Location[i] == ('Residence'):
        Total_num_males_residence += 1
        
    elif Gender[i] == ('Male') and Location[i] == ('Other'):
        Total_num_males_other += 1
        
    elif Gender[i] == ('Male') and Location[i] == (''):
        Total_num_males_unknown += 1

    elif Gender[i] == ('Female') and Location[i] == ('Hospital'):
        Total_num_females_hospital += 1
        
    elif Gender[i] ==('Female') and Location[i] == ('Residence'):
        Total_num_females_residence += 1
        
    elif Gender[i] == ('Female') and Location[i] == ('Other'):
        Total_num_females_other += 1
             
    elif Gender[i] == ('Female') and Location[i] == (''):
        Total_num_females_unknown += 1
        
    elif Gender[i] == ('') and Location[i] == (''):
        Total_num_unknown += 1