public
anonymous / ohio_election_plot.py
Created

cumulative vote tally plot

  • Download Gist
ohio_election_plot.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import pandas as pd
# df = pd.load('presdata.df')
# candidates = ['romney', 'perry', 'gingrich', 'paul', 'santorum'] # Ohio
# candidates = ['romney', 'huntsman', 'gingrich', 'paul', 'santorum', 'bachmann'] # Wisconsin
 
def plotCumTally(data, candidates, ix=None):
if ix is not None:
df = data.ix[ix].copy()
else:
df = data.copy()
 
df['total'] = sum(df[x] for x in candidates)
for x in candidates:
df[x + 'Percent'] = df[x] / df['total']
 
df['actualRank'] = df.TOTAL_VOTERS.rank(method='first')
# df['regRank'] = df.REG_VOTERS.rank(method='first')
df['actualInd'] = df.TOTAL_VOTERS.argsort()
# df['regInd'] = df.REG_VOTERS.argsort()
 
for x in candidates:
# df[x + 'CumActual'] = df.ix[df.actualInd][x].cumsum()
df[x + 'CumActual'] = df.sort(columns=['TOTAL_VOTERS'])[x].cumsum()
# for x in candidates:
# df[x + 'CumReg'] = df.sort(column='REG_VOTERS')[x].cumsum()
 
df['totalCumActual'] = sum(df[x + 'CumActual'] for x in candidates)
# df['totalCumReg'] = sum(df[x + 'CumReg'] for x in candidates)
 
for x in candidates:
df[x + 'CumActualPercent'] = df[x + 'CumActual'] / df['totalCumActual']
# for x in candidates:
# df[x + 'CumRegPercent'] = df[x + 'CumReg'] / df['totalCumReg']
 
df.plot(x='TOTAL_VOTERS', y='romneyCumActualPercent', ls='None', marker='.')
return df

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.