In [1]:
import pandas as pd
%pylab inline
In [2]:
"""
Results scraped from ECI site.
Code for the same can be found in the parent repo.
"""
df = pd.read_csv("eci-2014-states-candidate-wise.csv")
In [3]:
df = df[df['State-code']=='S13']
In [4]:
df.head(2)
Out[4]:
In [5]:
total_votes = df['Votes'].sum()
party_votes = df[['Votes', 'Party']].groupby('Party').sum().sort('Votes', ascending=False)
vote_shares = (100*party_votes/total_votes).reset_index()
# Top votes shares
vote_shares[:15]
Out[5]:
In [6]:
party_contestants = df.groupby('Party').size().order(ascending=False).reset_index()
# Number of contestants
party_contestants[:20]
Out[6]:
In [7]:
df_win = df.sort(['Constituency-code', 'Votes'], ascending=[1, 0]).groupby('Constituency-code', as_index=False).first()
df_win[:2]
Out[7]:
In [8]:
#apply(lambda t: t.iloc[1])
runners = df.sort(['Constituency-code', 'Votes'], ascending=[1, 0]).groupby('Constituency-code', as_index=False).nth(1).dropna()
df_win['Margin'] = df_win['Votes'] - runners['Votes'].values # no merge needed cos in same order
df_win['Runnerup-Party'] = runners['Party'].values
df_win[:2]
Out[8]:
In [9]:
party_wins = df_win.groupby('Party').size().order(ascending=False)
# Number of wins
party_wins
Out[9]:
In [10]:
runner_party = df_win.groupby('Runnerup-Party').size().order(ascending=False)
# Number of runner-up positions
runner_party
Out[10]:
In [11]:
df_alliance = df.copy()
alliances1 = ['Bharatiya Janata Party', 'Shivsena']
alliances2 = ['Indian National Congress', 'Nationalist Congress Party']
df_alliance['alliance'] = df_alliance['Party'].map(lambda x: 'BJP-SS' if x in alliances1 else 'INC-NCP'if x in alliances2 else x)
In [12]:
df_alliance[:4]
Out[12]:
In [13]:
alliance_local = df_alliance.groupby(['Constituency-code', 'Constituency', 'alliance'], as_index=False).sum()
In [14]:
alliance_wins = alliance_local.sort(['Constituency-code', 'Votes'], ascending=[1, 0])\
.groupby(['Constituency-code', 'Constituency'], as_index=False).first()
In [15]:
# If alliance worked, then wins would be
alliance_wins.groupby('alliance').size().order(ascending=False)
Out[15]:
In [16]:
code_votes = df.groupby('Constituency-code', as_index=False).sum()
code_votes.columns = ['Constituency-code', 'Total-Votes']
In [17]:
df_winp = df_win.merge(code_votes, on='Constituency-code')
df_winp['Win%'] = (100*df_winp['Votes']/df_winp['Total-Votes']).round(2)
In [18]:
# Winners with lower vote shares
df_winp.sort(['Win%'])[:5]
Out[18]:
In [19]:
# Winners with top vote shares
df_winp.sort(['Win%'], ascending=[0])[:5]
Out[19]:
In [20]:
df_winp['Margin%'] = (100*df_winp['Margin']/df_winp['Total-Votes']).round(2)
In [21]:
# Winners with lowest margin%
df_winp.sort(['Margin%'])[:5]
Out[21]:
In [22]:
# Winners with highest margin%
df_winp.sort(['Margin%'], ascending=[0])[:5]
Out[22]:
In [23]:
# with more than 50% voteshare sorted by margin
df_winp[df_winp['Win%']>50].sort('Margin%')[:5]
Out[23]:
In [24]:
# Parties which won with voteshares > 50%
df_winp[df_winp['Win%']>50].groupby('Party').size().order(ascending=False)
Out[24]:
In [25]:
# Parties which won with voteshares < 25%
df_winp[df_winp['Win%']<25].groupby('Party').size().order(ascending=False)
Out[25]:
In [26]:
# Parties which won with margin < 1%
df_winp[df_winp['Margin%']<1].groupby('Party').size().order(ascending=False)
Out[26]:
In [27]:
df_winp['First-Two'] = 2*df_winp['Votes']-df_winp['Margin']
df_winp['First-Two%'] = (100*df_winp['First-Two']/df_winp['Total-Votes']).round(2)
In [28]:
# Places where first-two had lesser share
df_winp.sort('First-Two%')[:3]
Out[28]:
In [29]:
# How parties dominated in mostly bi-polar regions?
df_winp[df_winp['First-Two%']>80].groupby('Party').size().order(ascending=False)
Out[29]:
In [30]:
# How parties dominated in mostly multi-cornered regions?
df_winp[df_winp['First-Two%']<50].groupby('Party').size().order(ascending=False)
Out[30]:
In [31]:
# Places where first-two had higher share
df_winp.sort('First-Two%', ascending=False)[:5]
Out[31]:
In [32]:
df = df.merge(code_votes, on='Constituency-code')
In [33]:
df['vote%'] = (100*df['Votes']/df['Total-Votes']).round(2)
# Places where party got less than 5% votes
for party in list(vote_shares['Party'][:10]):
print party, df[(df['Party']==party) & (df['vote%']<5)].shape[0]
In [34]:
alliance_local = alliance_local.merge(code_votes, on='Constituency-code')
In [35]:
alliance_local['vote%'] = (100*alliance_local['Votes']/alliance_local['Total-Votes']).round(2)
In [36]:
# Even under alliance, BJP-SS would have got less than 10% votes in
alliance_local[(alliance_local['alliance']=='BJP-SS') & (alliance_local['vote%']<10)]
Out[36]:
In [37]:
# Even under alliance, INC-NCP would have got less than 10% votes in
alliance_local[(alliance_local['alliance']=='INC-NCP') & (alliance_local['vote%']<10)]
Out[37]:
In [38]:
# Votes share distributions of MNS
df[(df['Party']=='Maharashtra Navnirman sena')]['vote%'].hist()
Out[38]:
In [39]:
# Votes share distributions of MIM
df[(df['Party']=='All India Majlis-E-Ittehadul Muslimeen')]['vote%'].hist()
Out[39]:
In [40]:
# high nota regions
notapa=df[(df['Party']=='None of the Above')][['Constituency-code','Constituency','Votes','vote%']]\
.merge(df_winp[['Candidate','Party','Runnerup-Party','Constituency-code','Votes','Margin','Total-Votes','Win%','Margin%','First-Two','First-Two%']],
on='Constituency-code', suffixes=('_nota', '_win'))
In [41]:
# Highe NOTA regions
notapa.sort('vote%', ascending=False)[:5]
Out[41]:
In [42]:
# Big NOTA affect at 20 places, places where candidate won by margin < NOTA votes
notapa[notapa['Votes_nota']>notapa['Margin']].sort('Margin%')
Out[42]:
In [43]:
# At these NOTA places, Shivsena won by a NOTA-whisker
notapa[notapa['Votes_nota']>notapa['Margin']].groupby('Party').size().order(ascending=False)
Out[43]:
In [44]:
PARTIES = alliances1 + alliances2 + ['None of the Above', 'All India Majlis-E-Ittehadul Muslimeen', 'Maharashtra Navnirman sena']
df_pivot_p = df[df['Party'].isin(PARTIES)].pivot(index='Constituency-code', columns='Party', values='Votes').reset_index()
df_pivot_p[:2]
Out[44]:
In [45]:
df_pivot_p = df_winp.merge(df_pivot_p, on='Constituency-code')
In [46]:
df_pivot_p[:2]
Out[46]:
In [47]:
combi = ['Shivsena', 'Maharashtra Navnirman sena']
df_sena_miss = df_pivot_p[((df_pivot_p[combi[0]].fillna(0) + df_pivot_p[combi[1]].fillna(0)) >
df_pivot_p['Votes']) &
(~df_pivot_p['Party'].isin(combi))]
In [48]:
print "SS-MNS additionally won - ", df_sena_miss.shape[0]
print "from"
df_sena_miss.groupby('Party').size().order(ascending=False)
Out[48]:
In [49]:
combi = ['All India Majlis-E-Ittehadul Muslimeen', 'Indian National Congress']
df_inc_mim = df_pivot_p[((df_pivot_p[combi[0]].fillna(0) + df_pivot_p[combi[1]].fillna(0)) >
df_pivot_p['Votes']) &
(~df_pivot_p['Party'].isin(combi))]
df_inc_mim
Out[49]:
In [50]:
combi = ['All India Majlis-E-Ittehadul Muslimeen', 'Nationalist Congress Party']
df_ncp_mim = df_pivot_p[((df_pivot_p[combi[0]].fillna(0) + df_pivot_p[combi[1]].fillna(0)) >
df_pivot_p['Votes']) &
(~df_pivot_p['Party'].isin(combi))]
df_ncp_mim
Out[50]:
In [51]:
combi = ['Indian National Congress', 'Nationalist Congress Party']
df_inc_ncp = df_pivot_p[((df_pivot_p[combi[0]].fillna(0) + df_pivot_p[combi[1]].fillna(0)) >
df_pivot_p['Votes']) &
(~df_pivot_p['Party'].isin(combi))]
print "INC-NCP additionally would have won", df_inc_ncp.shape[0]
print "eating from"
df_inc_ncp.groupby('Party').size().order(ascending=False)
Out[51]:
In [52]:
combi = ['Bharatiya Janata Party', 'Shivsena']
df_bjp_ss = df_pivot_p[((df_pivot_p[combi[0]].fillna(0) + df_pivot_p[combi[1]].fillna(0)) >
df_pivot_p['Votes']) &
(~df_pivot_p['Party'].isin(combi))]
print "BJP-SS would have won extra", df_bjp_ss.shape[0]
print "eating from"
df_bjp_ss.groupby('Party').size().order(ascending=False)
Out[52]:
In [53]:
top_independents = df[df['Party']=='Independent'].groupby('Constituency-code', as_index=False).first()
In [54]:
# At places where top independent got < 5% vote share
top_independents[top_independents['vote%']<5].shape[0]
Out[54]:
In [55]:
# At places where top independent got > 20% vote share
top_independents[top_independents['vote%']>20].shape[0]
Out[55]: