Question

In: Computer Science

Instructions: Create an empty dictionary: zip_dict = {} Loop through all the zip_codes in df_zip Create...

Instructions:

  • Create an empty dictionary: zip_dict = {}
  • Loop through all the zip_codes in df_zip
  • Create a dictionary key for the first 3 digits of a zip_code in zip_dict
  • Continually add population counts to the key that contains the same first 3 digits of the zip code

To extract the population you will find this code useful:

population = list(df_zip.loc[df_zip['zip'] == zip_code]['population'])

To extract the first 3 digits of a zip_code you will find this code useful:

int(str(zip_code)[:3])

Solutions

Expert Solution

#python code

zip_dict = {}

for index,row in df_zip.iterrows():

      zip_code = row['zip']

      population = df_zip.loc[df_zip['zip'] == zip_code,'population']

     if zip_code in zip_dict:

         zip_dict[int(str(zip_code)[:3])] =zip_dict[int(str(zip_code)[:3])] + population     #[adds if already exists]

     else:

        zip_dict[int(str(zip_code)[:3])] = population       #create new key if not present in dictionary

                                               ###Thank You.......


Related Solutions

*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names...
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names to addresses. A person's name (stored as a string) will be the KEY and that person's address (stored as a string) will be the VALUE. 1.2) Insert into the dictionary 'addresses' the names and addresses of two (possibly imaginary) friends of yours. 1.3) Create a second empty dictionary called 'ages'. The dictionary you just created will map names to ages. A person's name (stored...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
ET574 Dictionary (Chapter 6) 1) Create a dictionary containing the following pieces of information about a...
ET574 Dictionary (Chapter 6) 1) Create a dictionary containing the following pieces of information about a student: Her name is Emmylou; She is on track to graduate in Fall 2021; Her bill is paid; Her major is Archeology; She belongs to these school clubs – Photography, Acting and Glee (Your job as the programmer is to choose the best data type as well as any key names you will use within the program) 2) Do the same for another student:...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist; 2. add_new_item(dictionary,key,value): a function that adds...
Create the following java program with class list that outputs: //output List Empty List Empty List...
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is...
True or False: It's easy to loop through an array using a for loop in C++...
True or False: It's easy to loop through an array using a for loop in C++ because you can use the .size() function to get the total number of spaces in the array True or False: It's easy to loop through a vector using a for loop in C++ because you can use the .size() function to get the total number of spaces in the vector
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Assignment 2- 9   USING A FOR LOOP Create a PL/SQL block using a FOR loop to...
Assignment 2- 9   USING A FOR LOOP Create a PL/SQL block using a FOR loop to generate a payment schedule for a donor’s pledge, which is to be paid monthly in equal increments. The values variable for the block is starting payment due date, monthly payment amount, and a number of total monthly payments for the pledge. The list that’s generated should display a line for each monthly payment showing payment number, date due, payment amount, and donation balance (remaining...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
ORACLE TASK 2-2 USING A FOR LOOP Create a PL/SQL block using a FOR loop to...
ORACLE TASK 2-2 USING A FOR LOOP Create a PL/SQL block using a FOR loop to generate a payment schedule for a donor’s pledge, which is to be paid monthly in equal increments. Values variable for the block are starting payment due date, monthly payment amount and number of total monthly payments for the pledge. The list that’s generated should display a line for each monthly payment showing payment number, date due, payment amount, and donation balance (remaining amount of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT