Data analysis case -- data analysis of 2012 US presidential campaign sponsorship

An analysis of the data of US presidential campaign sponsorship

This article is from Alibaba cloud Tianchi laboratory, Original address of case
For reference, Xiao Wang, a self-taught data analysis student, wrote and analyzed the data by himself. The codes and results are as follows
1. Import related python data analysis library

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

2. Data loading and overview
2.1 because the single data is too large, we divide the data source into three 0-50w lines, 50-100W lines and 100W + lines

#data fetch
data_01=pd.read_csv(r'H:\Ali cloud\2012 An analysis of the data of US presidential campaign sponsorship\data_01.csv')
data_02=pd.read_csv(r'H:\Ali cloud\2012 An analysis of the data of US presidential campaign sponsorship\data_02.csv')
data_03=pd.read_csv(r'H:\Ali cloud\2012 An analysis of the data of US presidential campaign sponsorship\data_03.csv')

2.2 data display:
Read the first five lines of data

Read the first five lines of data
Read the first five lines of data
2.3 consolidated data (data ABCD 01, data ABCD 02, data ABCD 03)


2.4 view data information, including the name, non empty quantity and data type of each field

We can see that the number of fields in the two columns of contbr ﹣ employee and contbr ﹣ occupation is a little less, indicating that there are empty values in them
2.5 view the summary of data


2.6 treatment of missing value
From, we can see that there are a small number of missing values in contbr? Employee and contbr? Occupation, which we fill with not provided

data['contbr_employer'].fillna('not provided',inplace=True)
data['contbr_occupation'].fillna('not provided',inplace=True)

2.7 view of missing values


As you can see, there is no missing value
2.8 check who are the presidential candidates in the data

print('Share{}Candidates, respectively'.format(len(data['cand_nm'].unique())))

2.9 through search engine and other ways, obtain the Party of each presidential candidate, establish dictionary parties, with candidate name as key and party as corresponding value

parties = {'Bachmann, Michelle': 'Republican',
           'Cain, Herman': 'Republican',
           'Gingrich, Newt': 'Republican',
           'Huntsman, Jon': 'Republican',
           'Johnson, Gary Earl': 'Republican',
           'McCotter, Thaddeus G': 'Republican',
           'Obama, Barack': 'Democrat',
           'Paul, Ron': 'Republican',
           'Pawlenty, Timothy': 'Republican',
           'Perry, Rick': 'Republican',
           "Roemer, Charles E. 'Buddy' III": 'Republican',
           'Romney, Mitt': 'Republican',
           'Santorum, Rick': 'Republican'}

2.10 add a column of parties to store party information through map mapping function

data['party']=data['cand_nm'].map(parties)#map mapping
#Look at the two parties

#It can be seen that the total amount of sponsorship received by the Republican Party is higher, and the number of sponsorship received by Democrat is higher

2.11 check the data sheet

Meaning of each field name
Can? Nm – name of the candidate receiving the donation
contbr_nm - donor name
contbr_st – donor's state
Contbr? Employer – donor's company
Contb ﹐ receive ﹐ AMT – donation amount (USD)
Contb · receive · DT – date of receipt of donation

2.12 sorting: sort the total sponsorship amount according to the occupation summary, summarize it according to the position, calculate the total sponsorship amount, display the top 20 items, and find that many professions are the same, just different expressions, such as C.E.O. and CEO, are all one occupation


2.13 data conversion using functions: analysis of occupation and employer information
Many occupations involve the same basic type of work. Let's clean up the data (dict.get is cleverly used here to allow non mapping occupations to "pass")

#Set up a profession corresponding dictionary, map different expressions of the same profession to corresponding professions, for example, map C.E.O. to CEO
occupation_map = {

# If not in dictionary, returns x
f = lambda x: occupation_map.get(x, x)
data.contbr_occupation =
#Data.contbr occupation is equivalent to the x in the above statement
#Data.contbr ﹣ the result returned by the location is mapped with the key in the location ﹣ map. If the same value is returned, the corresponding value is returned. If the different values are returned, the default value is returned
#contbr_occupation - donor occupation

Similarly, similar transformation of employer information

emp_mapping = {

# If no mapping provided, return x
f = lambda x: emp_mapping.get(x, x)
data.contbr_employer =

Let's demonstrate the use of dict.get()

dict_data ={1:'one',2:'two',3:'three',4:'four'}

Output result

 # get method of dictionary
        # For example: list.get(k,d), where get is equivalent to an if...else... Statement, if the parameter k is in the dictionary, the dictionary will return list[k]; if the parameter k is not in the dictionary, then it will return the parameter D, if the parameter k is in the dictionary, then it will return the value value corresponding to K
        # l = {5:2,3:4}
        # The value returned by print l.get(3,0) is 4;
        # The return value of Print l.get (1,0) is 0

3.1 data filtering
Sponsorship includes refunds (negative contributions). To simplify the analysis process, we limit the dataset to only positive contributions

data = data[data['contb_receipt_amt']>0]

3.2 check the total amount of sponsorship received by each candidate, contb ﹣ receive ﹣ AMT – donation amount (USD), and can ﹣ nm – name of the candidate receiving the donation


As can be seen from the above, sponsorship is mainly between Obama and Romney. In order to better focus on the competition between the two, we select the data subsets of these two candidates for further analysis
3.3 select a subset of data whose candidates are Obama and Romney

data_vs = data[data['cand_nm'].isin(['Obama, Barack','Romney, Mitt'])].copy()
#data['cand_nm'].isin(['Obama, Barack','Romney, Mitt']) returns a bool type value
# data[bool] returns specific data, all of which are true
#data.copy() copies the returned data

3.4 discrete data, discrete data with cut

bins = np.array([0,1,10,100,1000,10000,100000,1000000,10000000])
labels = pd.cut(data_vs['contb_receipt_amt'],bins)

3.6 summarize the sponsorship amount according to the party and occupation, similar to the pivot table operation in excel, and the aggregate function is sum

by_occupation = data.pivot_table('contb_receipt_amt',index='contbr_occupation',columns='party',aggfunc='sum')
#Filter out data with sponsorship amount less than 200W

over_2mm = by_occupation[by_occupation.sum(1)>2000000]

3.7 drawing


3.8 grouping based on occupation and employer information
Let's take a look at the occupations and employers with the highest total contributions to Obama and Romney. Note that dict.get is cleverly used here to allow careers without mapping to "pass"

def get_top_amounts(group,key,n=5):
#Pass in the object after groupby grouping, and return the data before sorting summarized by key field
    totals = group.groupby(key)['contb_receipt_amt'].sum()
    return totals.sort_values(ascending=False)[:n]
grouped = data_vs.groupby('cand_nm')

3.9 similarly, use get top amounts() to analyze the employer


3.10 labels is the Series after the amount of sponsorship is discretized

grouped_bins = data_vs.groupby(['cand_nm',labels])

3.11 next, we will count the sponsorship amount of each interval


Insert a small experiment

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

Use the stack function to convert the row index of data ['one', 'two', 'three'] into column index (the second layer), and then you get a hierarchical Series (data2). Use the unstack function to convert the column index of the second layer of data2 into row index (the default can be changed), and then you get DataFrame (data3)
This experiment mainly demonstrates the stack function and unstack function

3.12 total sponsorship amount of Obama and Romney


3.13 calculate the proportion of the total amount of sponsorship received by two candidates in each interval

normed_sums = bucket_sums.div(bucket_sums.sum(axis=1),axis=0)

3.14 using histogram and specifying stacked=True to stack, the percentage stacked graph can be completed
It can be seen that in terms of micro sponsorship, Obama received much more amount and quantity than Romney
3.15 count the top 20 sponsors by name


4.1 STR to datatime
We can use the to "datetime method to parse a variety of different date representations. The parsing of standard date formats (such as ISO8601) is very fast. We can also specify a specific date resolution format, such as PD. To "datetime (series, format = '% Y%m%d')

data_vs['time'] = pd.to_datetime(data_vs['contb_receipt_dt'])

4.2 time as index


4.3 resampling and frequency conversion

Resampling refers to the process of changing the frequency of time series into another frequency. Changing high-frequency data to low-frequency data is called downsampling. Example groups the data and then calls the aggregate function. Here we convert the frequency from daily to monthly, which belongs to the sampling of high-frequency to low-frequency.

vs_time = data_vs.groupby('cand_nm').resample('M')['cand_nm'].count()

4.4 by comparing the number of sponsorship received by the two presidential candidates from April, 2011 to April, 2012, we can see that the closer to the election, the higher the enthusiasm for sponsorship, and Obama has an absolute advantage in all periods

fig1, ax1=plt.subplots(figsize=(32,8))

Published 2 original articles, won praise 1, visited 32
Private letter follow

Tags: Lambda less Python Excel

Posted on Thu, 12 Mar 2020 05:46:17 -0700 by sam2004