<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")
%matplotlib inline
from __future__ import division
import requests
from io import StringIO
url = "https://elections.huffingtonpost.com/pollster/2012-general-election-romney-vs-obama.csv"

source = requests.get(url).text

poll_data = StringIO(source)
poll_df = pd.read_csv(poll_data)

poll_df.info()
poll_df.head()
sns.catplot(x='Affiliation', data = poll_df, kind='count')
sns.catplot(x='Affiliation', data=poll_df, hue='Population', kind='count')
avg = pd.DataFrame(poll_df.mean())

avg.drop('Number of Observations', axis=0, inplace=True)
avg.head()
std = pd.DataFrame(poll_df.std())

std.drop('Number of Observations', axis=0, inplace=True)
std.head()
poll_df.plot( )
avg.plot(yerr=std, kind='bar', legend=False)
poll_avg = pd.concat([avg, std], axis=1)
poll_avg.columns = ['Average', 'STD']
poll_avg
poll_df.plot(x='End Date', y=['Obama', 'Romney', 'Undecided'], linestyle='', marker='o')
from datetime import datetime
poll_df['Difference'] = (poll_df.Obama - poll_df.Romney)/100
poll_df.head()
poll_df.plot('Start Date', 'Difference', figsize=(12,4), marker='o', linestyle='-', color='purple')
row_in = 0
xlimit = []

for date in poll_df['Start Date']:
  if date[0:7] == '2012-10':
    xlimit.append(row_in)
    row_in += 1
  else:
    row_in += 1

print(max(xlimit))
print(min(xlimit))

poll_df.plot('Start Date', 'Difference', figsize=(12,4), marker='o', linestyle='-', color='purple', xlim=(18,100))

#Oct 3rd

plt.axvline(x=18+2, linewidth=4, color='red')

plt.axvline(x=18+10, linewidth=4, color='blue')

plt.axvline(x=18+21, linewidth=4, color='green')

from google.colab import drive
drive.mount('/gdrive')
donor_df = pd.read_csv('/gdrive/My Drive/Election_Donor_Data.csv')
donor_df.info()
donor_df.head()
donor_df['contb_receipt_amt'].value_counts()
max(donor_df['contb_receipt_amt'])
don_mean = donor_df['contb_receipt_amt'].mean()

don_std = donor_df['contb_receipt_amt'].std()

print("The average donation was %.2f with a std %.2f" %(don_mean, don_std))
top_donor = donor_df['contb_receipt_amt'].copy()

top_donor = top_donor.sort_values(ascending=False)
top_donor = top_donor.sort_values(ascending=False)
top_donor = top_donor[top_donor > 0]
top_donor.value_counts()
top_donor = top_donor.sort_values(ascending=False)
com_don = top_donor[top_donor < 2500]

com_don.hist(bins=100)
candidates = donor_df.cand_nm.unique()

candidates
# Dictionary of party affiliation
party_map = {'<NAME>': 'Republican',
           '<NAME>': 'Republican',
           '<NAME>': 'Republican',
           '<NAME>': 'Republican',
           '<NAME>': 'Republican',
           'McCotter, <NAME>': 'Republican',
           '<NAME>': 'Democrat',
           '<NAME>': 'Republican',
           '<NAME>': 'Republican',
           '<NAME>': 'Republican',
           "<NAME>. 'Buddy' III": 'Republican',
           '<NAME>': 'Republican',
           '<NAME>': 'Republican'}

donor_df['Party'] = donor_df.cand_nm.map(party_map)
donor_df.head()
donor_df = donor_df[donor_df.contb_receipt_amt > 0]
donor_df.head()
donor_df.groupby('cand_nm')['contb_receipt_amt'].count()
donor_df.groupby('cand_nm')['contb_receipt_amt'].sum()
cand_amount = donor_df.groupby('cand_nm')['contb_receipt_amt'].sum()

i = 0

for don in cand_amount:
  print('the candidate %s raise %.0f dollars' %(cand_amount.index[i],don))
  print("\n")
  i  += 1

cand_amount.plot(kind='bar')
donor_df.groupby('Party')['contb_receipt_amt'].sum().plot(kind='bar')
occupation_df = donor_df.pivot_table('contb_receipt_amt', index='contbr_occupation', columns = 'Party', aggfunc='sum')
occupation_df.tail()
occupation_df.head()
occupation_df.shape
occupation_df = occupation_df[occupation_df.sum(1) > 1000000]
occupation_df.shape
occupation_df.plot(kind='bar')
occupation_df.plot(kind='barh', figsize=(10,12), cmap='seismic')
occupation_df.drop(['INFORMATION REQUESTED PER BEST EFFORTS', 'INFORMATION REQUESTED'], axis=0, inplace=True)
occupation_df.loc['CEO'] = occupation_df.loc['CEO'] + occupation_df.loc['C.E.O.']
occupation_df.drop('C.E.O.', inplace=True)
occupation_df.plot(kind='barh', figsize=(10,12), cmap='seismic')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17430765650453806, 'fraction_numerical': 0.018152199208750292, 'mean_word_length': 6.0, 'pattern_counts': {'":': 1, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '525302', 'license': 'MIT', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1662, 'n_words': 283, 'path': 'ElectionAnalysis.ipynb', 'repo_name': 'tarun-jethwani/DataAnalysesNotebooks', 'size': '4397'}