Question: In jupyter, Python 3: 1- Visit this website: https://www.ssa.gov/oact/babynames/limits.html 2- download the first file, National data (7Mb) 3- Unzip the folder and upload all the
In jupyter, Python 3:
1- Visit this website: https://www.ssa.gov/oact/babynames/limits.html 2- download the first file, National data (7Mb) 3- Unzip the folder and upload all the .txt files to the jupyter. Now, you are ready to finish the code.
The purpose of this code is finding the top names of babys based on their gender from 1880 upto 2017. The function is developed based on the materials covered in the lectures. Nothing different than course material. This is beauty of the python that you can do a lot of complex computation using realtive simple codes. :-)
'?' Top10List(Gender,n): # Define a function import pandas as pd # import pandas Name = '?' # create a list Names= '?' # create a dictionary for i in range(1880,2018): filename = 'yob'+'?'(i)+'.txt' # convert i to a datatype that can be used in open function with open(filename) as f: text = f.'?'() # read the file text = text.strip(' ') text = text.split() for line in text[:1000]: words = line.'?'(',') # breakdown components of each line, they are seperated by ',': Name, Sex, #of Given Names if words['?'] == Gender: # here, we check whether gender in the the above line is equal to the request Gender. if words[0] '?' Name: # if the name is in the list of Name. Names[words[0]][i]= int(words[2]) # this line adds a key-value pair to a key in the Names dictionary '?': # otherwis conidtion Name.append(words[0]) # this is a new name and its added to the Name list. Names[words[0]] = {} # in the Names dictionary, key equal to the name is created. Names[words[0]][i]= int(words[2]) dfNames = '?'(Names) # converte the dictionary to a dataframe. dfNames = dfNames.'?' # fill the NaN values with 0. Result = dfNames.sum() # for each column find the sum, given the column header is babyNames. FianlResult = Result.sort_values(ascending=False).'?' # show the first n rows of the Result Series. '?' FianlResult # the missing keyword is associted with function and shows the output
Top10List(n = 25, Gender = 'M') # the first call of the function:
Top10List(n = 30, Gender = 'F') # the 2nd call of the function:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
