Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

image text in transcribed
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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

\f

Answered: 1 week ago