NEED HELP ON MY PYTHON PROBLEM. in this exercise im using Pandas
-ive been completely stuck on this many hours and in desperate need of guidance.
Below is a screen shot of the problem set i am working on. I was given a data frame containing data for donations to the presidential candidates of 2016. The first part required me to find the top 5 occupations that donated the most to Hillary clinton's campaign. (which i did successfully)
The second part, i need to wrap that work into a function called "get_donors" so it can be applied to other candidates. I cam completely stuck on this part and in dire need of a lifeline PLEASE SOMEONE HELP ME ! Below is a screen shot of the work ive done so far.
3. Exploring Donor Occupations (30 points) Above in part 2, we saw that some simple data analysis can give us insights into the campaigns of our candidates. Now let's quickly look to see what kind of person is donating to each campaign using the contbr_occupation variable. In [11]: 1 #filter dataframe for Hillary Clinton hillary = contribu (contribcand_nm == "Clinton, Hillary Rodham") ] #apply value_counts to contr_occupation which will output a series occup = hillary . contbr_occupation. value_counts ( ) 6 7 #convert series to of 8 df4 = occup. to_frame ( ) 9 10 #rename total contributions to count 11 df4 . columns = [ 'count' ] 12 13 #use the head function to display the top 5 occupations 14 df4 . head(5) Out [ 11] : count RETIRED 33450 ATTORNEY 7315 INFORMATION REQUESTED 4742 TEACHER 3591 HOMEMAKER 3436 HELP 3b. Write a function called get_donors . Imagine that you want to do the previous operation on several candidates. To keep your work neat, you want to take the work you did on the Clinton-subset and wrap it in a function that you can apply to other subsets of the data. The function should take a DataFrame as a parameter, and return a Series containing the counts for the top 5 occupations contained in that DataFrame. In [ ] : def get_donors (df) : """This function takes a dataframe that contains a variable named contbr_occupation. It outputs a Series containing the counts for the 5 most common values of that variable." ". contbr_occupation = df. contbr_occupation. value_counts ( ) return contbr_occupation . head(5) HELP-> 3c. Now run the get_donors function on subsets of the dataframe corresponding to three candidates. Show each of the three candidates below. . Hillary Clinton Bernie Sanders . Donald Trump In [ ]: 1 #hillary clinton df hillary_of = contribcontrib. cand_nm == "Clinton, Hillary Rodham" ) ] 3 hillary . apply (get_donors) 4 hillary_df In [ ]: 1 #Bernie Sanders of bernie_df = contribcontrib. cand_nm == "Sanders, Bernard" ) ] bernie_df . applymap (get_donors In [ ]: #Donald Trump df donald_df = bernie_df = contribcontrib. cand_nm == "Trump, Donald J.") ] AW donald_df . applymap (get_donors ) HELP 3d. Finally, use groupby to separate the entire dataset by candidate. Call .apply(get_donors) on your groupby object, which will apply the function you wrote to each subset of your data. . Look at your output and marvel at what pandas can do in just one line