Question
# Need help with typing up code to plot from the given dataset provided below, cannot use any other packages; Must be in Python. #
# Need help with typing up code to plot from the given dataset provided below, cannot use any other packages; Must be in Python.
# Images show how the end result should look like per question
%matplotlib inline import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib import rcParams import seaborn as sns
# This is a randomly-sample subset of the full data set. df = pd.read_csv("https://raw.githubusercontent.com/grbruns/cst383/master/campaign-ca-2016-sample.csv")
'''Let's look at the amount of the contributions, instead of the number of contributions. Which candidates had the highest median contribution amounts?
'''
#@1 Show the median contribution amount by candidate.
#CODE HERE
''' What is the distribution of the contribution amounts? There are a small number of very large amounts, which make it hard to display the distribution. Also, there are some negative contribution amounts that seem to reflect returned contributions. Therefore, let's focus on contributions ranging from 0 to 3,000 dollars.
'''
#@ 2 Create a histogram showing contribution amounts. Use # contributions from 0 - 3000 dollars only. Create the # histogram with Seaborn.
#CODE HERE
''' It appears that most contributions are small. Let's restrict our attention to an even smaller range of contributions to get a better idea of how small contributions are distributed. '''
#@ 3 Create a histogram showing contribution amounts. Use # contributions from 0 - 500 dollars only. Create the # histogram with Seaborn.
#CODE HERE
''' The appearance of a histogram is sensitive to the number of bins that are used and where the bin edges lie. Let's look at the contribution amounts again using a density plot. '''
#@ 4 Create a density plot (sometimes called a kernel density # plot) showing contribution amounts. Use contributions from # 0 - 500 dollars only. Create the density plot with Seaborn. # histogram use Seaborn. # Hint: you may want to start by creating a series containing # the contb_receipt_amt values from 0-500.
#CODE HERE
''' Let's compare the size of contributions between candidates Rubio and Cruz. Did one of them tend to get larger-sized contributions? '''
#@ 5 Create a "double density plot" showing the contributions for # Rubio and Cruz. Use contributions in the range of 0-1000 dollars # only. Be sure to include a legend. # Hint: you can create two series, one for 0-1000 contributions to # Rubio, and another for 0-1000 contributions to Cruz. # Remember that you can superimpose plots by simply plotting one # after another.
#CODE HERE
'''Rubio and Cruz were Republican candidates. Let's look at a pair of Democratic candidates.'''
#@ 6 Show the contributions of 0-1000 for Clinton and Sanders. # Use a seaborn violin plot. # Hint: create a modified version of the data frame that contains only # contributions for Sanders and Clinton, and only contains contributions # from 0 to 1000 dollars. Then use Seaborn's violinplot.
#CODE HERE
''' Which occupations are associated with the greatest number of contributions? This will be interesting, but we need to keep in mind that the occupation with the greatest number of contributions might just be the most common occupation. '''
#@ 7 Create a bar plot showing th total number of contributions by occupation, # for the 10 occupations with the largest number of contributions. Use # Pandas for the bar plot. Limit the occupation names to 18 characters. # Hint: to limit the occupation names to 18 characters, you can create a # new column 'short_occ' by using pd.Series.str.slice on the # 'contbr_occupation' column.
#CODE HERE
''' We can classify contributors as either employed, unemployed, or retired. Among these groups, which makes the most contributions? '''
#@ 8 Create a new column "employment_status", derived from the # contbr_occupation column. The value of employment_status should # be "EMPLOYED" if contbr_occupation is not "RETIRED" or "NOT EMPLOYED", # and should be the original contbr_occupation otherwise. Show the # number of contributions by employment status as a bar plot. # Hint: to create the new column, consider creating a function that # takes as input a contbr_occupation value and returns an employment # status value. Then use this function with 'apply'.
#CODE HERE
Contribution amount ($) 1000 500 1500 2000 2500 o 1 Candidate Santorum, Richard J. Sanders, Bernard Carson, Benjamin S. Cruz, Rafael Edward 'Ted', Jindal, Bobby Paul, Rand Lessig, Lawrence Clinton, Hillary Rodham . Fiorina, Carly - Huckabee, Mike Rubio, Marco Stein, Jill Trump, Donald J. Walker, Scott Webb, James Henry Jr. O'Malley, Martin Joseph Kasich, John R. Graham, Lindsey O. Christie, Christopher J. Bush, Jeb Pataki, George E. Perry, James R. (Rick) Median campaign contribution by candidate Contribution amounts (contributions from 0-$3000 only) 10000 8000 6000 4000 2000 0 0 3000 1000 2000 Contribution(dollars) Contribution amounts (contributions from 0-$500 only) 3000 2000 1000 0 0 500 100 200 300 400 Contribution(dollars) Contribution amounts (contributions from 0-$500 only) 0.0125 0.0100 0.0075 0.0050 0.0025 w ^ 0.0000 0 200 400 Contribution(dollars) Contributions of $0-1000 for Rubio and Cruz Rubio 0.010 Cruz 0.008 0.006 0.004 0.002 0.000 0 1000 250 500 750 Contribution in $ Contributions of $0-1000 for Sanders and Clinton 1000 750 Contribution amount ($) 500 250 0 Sanders, Bernard Clinton, Hillary Rodham Candidate Number of contributions by occupation (top 10 occupations) 4000 3000 2000 1000 o RETIRED TEACHER HOMEMAKER ENGINEER NOT EMPLOYED ATTORNEY INFORMATION REQUES PHYSICIAN NONE SOFTWARE ENGINEER Number of contributions by employment status 12500 10000 7500 5000 2500 0 EMPLOYED RETIRED NOT EMPLOYEDStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started