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...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while...
Implement a python program in file named tarvel.py. Create an empty dictionary named responses. Implement while loop to take in user's name and desired destination for as long as there are user inputs. Prompt user to input yes to continue and no to quit. Prompt for user's name. Receive the name into the program and save it as the value of name variable. Prompt user for their desired vacation destination. Receive response and save it as the value of a...
C++ needed Create a loop which gives the player instructions on what to input. Then read...
C++ needed Create a loop which gives the player instructions on what to input. Then read input from the player. The input will be either one of three options: • If the user enters the word “answer” or some other string you choose to indicate the player is ready to end the game and guess. In this case, output the hidden rule. • Three numbers separated by spaces. Let’s call a trio of numbers and the corresponding output a Guess....
Instructions Complete the lab using “for loop”. Do not write the code in multiple programs. All...
Instructions Complete the lab using “for loop”. Do not write the code in multiple programs. All the 3 methods should be written in 1 program. Write a java program calls the following methods: printStars(): Takes an int (n) as parameter and prints n stars (*) using for loop. Multiples(): Takes an int (n) as parameter and prints first 10 multiples n in a single line using for loop. hasAnEvenDigit: Takes an int (n) as parameter and returns whether n has...
Use a For loop to compute the sum of all the odd numbers from 1 through...
Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic for zyBook Challenge activity 4.6.1 (Nested loops: Indent text) so that the first line is indented userNum...
Complete the PA7_incomplete.py program (posted below) for an infinite interactive loop to process a dictionary. The...
Complete the PA7_incomplete.py program (posted below) for an infinite interactive loop to process a dictionary. The program starts by asking an input file name where the dictionary entries are stored. This file will be assumed be present and to contain two items in each line as key and value pairs separated by a comma, like the example.txt file (posted below). The file could be possibly empty. The program reads the contents of this file into a dictionary, closes the file...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT