Skip to content

Instantly share code, notes, and snippets.

Created October 30, 2012 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3978406 to your computer and use it in GitHub Desktop.
Save anonymous/3978406 to your computer and use it in GitHub Desktop.
cumulative vote tally plot
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment