Maharashtra Assembly Elections - The Crest and Trough of a Wave

Analysis done for The Hindu's article on The Crest and Trough of a Wave

Pratap Vardhan


In [1]:
import pandas as pd

In [2]:
_FOURL = ['Bharatiya Janata Party','Shivsena','Indian National Congress','Nationalist Congress Party']
_FOURS = ['BJP','SHS','INC','NCP']

In [3]:
df = pd.read_csv("eci-2014-states-candidate-wise.csv")
df = df[df['State-code']=='S13']
df = df.drop(['State','State-code'], 1)
total_votes = df['Votes'].sum()
df_4 = df[df['Party'].isin(_FOURL)]

In [4]:
df_old = pd.read_csv("old.csv")[['AC_NO', 'NAME', 'AC_NAME','PARTY','VOTES']]
df_old.columns = ['Constituency-code', 'Candidate', 'Constituency', 'Party', 'Votes']
total_votes_old = df_old['Votes'].sum()
df_old_4 = df_old[df_old['Party'].isin(_FOURS)]

In [5]:
code_votes = df.groupby('Constituency-code', as_index=False).sum()
code_votes.columns = ['Constituency-code', 'Total-Votes']

In [6]:
code_votes_old = df_old.groupby('Constituency-code', as_index=False).sum()
code_votes_old.columns = ['Constituency-code', 'Total-Votes']

In [7]:
df_4 = df_4.merge(code_votes_old, on='Constituency-code')
df = df.merge(code_votes, on='Constituency-code')
df_old_4 = df_old_4.merge(code_votes_old, on='Constituency-code')
df_old = df_old.merge(code_votes_old, on='Constituency-code')

In [8]:
df_4.head(2)


Out[8]:
Candidate Party Votes Constituency Constituency-code Total-Votes
0 PADAVI ADV. K.C. Indian National Congress 64410 Akkalkuwa 1 143301
1 PARADAKE VIJAYSING RUPSING Nationalist Congress Party 48635 Akkalkuwa 1 143301

In [9]:
df_old_4.head(2)


Out[9]:
Constituency-code Candidate Constituency Party Votes Total-Votes
0 1 PADVI ADV K. C. Akkalkuwa INC 52273 143301
1 1 VALVI MANGALSING KOMA Akkalkuwa SHS 6184 143301

In [10]:
_4CONTEST = df_old_4.groupby('Party').size().order(ascending=False).reset_index()
_4CONTEST.columns = ['Party', 'Seats']

In [11]:
party_votes_old = df_old[['Votes', 'Party']].groupby('Party', as_index=False).sum().sort('Votes', ascending=False)
party_votes_old['vote%'] = (100*party_votes_old['Votes']/total_votes_old).round(2)
party_votes_old[party_votes_old['Party'].isin(_FOURS)].merge(_4CONTEST, on='Party')


Out[11]:
Party Votes vote% Seats
0 INC 9521703 21.01 170
1 NCP 7420212 16.37 113
2 SHS 7369030 16.26 160
3 BJP 6352147 14.02 119

In [12]:
WHERE4TOTALVOTES = []
_THE4 = party_votes_old[party_votes_old['Party'].isin(_FOURS)].merge(_4CONTEST, on='Party')
for party in _THE4['Party'].values:
    WHERE4TOTALVOTES.append(df_old[df_old['Constituency-code'].isin(df_old_4[df_old_4['Party']==party]['Constituency-code'])].sum()['Votes'])
_THE4['a-votes'] = WHERE4TOTALVOTES
_THE4['a-vote%'] = (100*_THE4['Votes']/_THE4['a-votes']).round(2)
_THE4


Out[12]:
Party Votes vote% Seats a-votes a-vote%
0 INC 9521703 21.01 170 26141728 36.42
1 NCP 7420212 16.37 113 18480764 40.15
2 SHS 7369030 16.26 160 24711167 29.82
3 BJP 6352147 14.02 119 18975055 33.48

In [13]:
df_old_4_piv = df_old_4.pivot(index='Constituency-code', columns='Party', values='Votes').reset_index()

In [14]:
# direct fights
for i in range(len(_FOURS)):
    for j in range(i+1,len(_FOURS)):
        print _FOURS[i], _FOURS[j], (df_old_4_piv[_FOURS[i]]+df_old_4_piv[_FOURS[j]]).count()


BJP SHS 0
BJP INC 78
BJP NCP 39
SHS INC 86
SHS NCP 71
INC NCP 0

In [15]:
df_old_4_piv[:4]


Out[15]:
Party Constituency-code BJP INC NCP SHS
0 1 NaN 52273 NaN 6184
1 2 NaN 51222 NaN 38635
2 3 75465 NaN 99323 NaN
3 4 6166 74024 NaN NaN

In [16]:
def funcfour(df_4, df_old_4, party):
    temp = df_4[df_4['Constituency-code'].isin(df_old_4[df_old_4['Party']==party]['Constituency-code'].values)]
    temp['Seats'] = 1
    temp = temp.groupby('Party', as_index=False).sum()[['Party','Seats','Votes','Total-Votes']]
    temp['Vote%'] = (100*temp['Votes']/temp['Total-Votes']).round(2)
    return temp

In [17]:
funcfour(df_4, df_old_4, 'BJP')


Out[17]:
Party Seats Votes Total-Votes Vote%
0 Bharatiya Janata Party 111 8380695 17624130 47.55
1 Indian National Congress 119 4355273 18975055 22.95
2 Nationalist Congress Party 117 3439150 18616004 18.47
3 Shivsena 117 2340840 18653500 12.55

In [18]:
funcfour(df_4, df_old_4, 'SHS')


Out[18]:
Party Seats Votes Total-Votes Vote%
0 Bharatiya Janata Party 143 6061470 21837664 27.76
1 Indian National Congress 160 4842788 24711167 19.60
2 Nationalist Congress Party 153 5331786 23599272 22.59
3 Shivsena 158 7463547 24494272 30.47

In [19]:
funcfour(df_4, df_old_4, 'INC')


Out[19]:
Party Seats Votes Total-Votes Vote%
0 Bharatiya Janata Party 159 9284597 24381069 38.08
1 Indian National Congress 170 7532763 26141728 28.82
2 Nationalist Congress Party 163 3031270 24964970 12.14
3 Shivsena 165 5273551 25435024 20.73

In [20]:
funcfour(df_4, df_old_4, 'NCP')


Out[20]:
Party Seats Votes Total-Votes Vote%
0 Bharatiya Janata Party 96 5145570 15464542 33.27
1 Indian National Congress 112 1932385 18267871 10.58
2 Nationalist Congress Party 112 6026252 18325221 32.89
3 Shivsena 112 4776652 18309458 26.09

In [21]:
df['vote%'] = (100*df['Votes']/df['Total-Votes']).round(2)

In [22]:
for party in _FOURL:
    print party, df[(df['Party']==party) & (df['vote%']<5)].shape[0]


Bharatiya Janata Party 12
Shivsena 54
Indian National Congress 74
Nationalist Congress Party 89

In [23]:
df_4_piv = df_4.pivot(index='Constituency-code', columns='Party', values='Votes').reset_index()

In [24]:
df_4_piv[df_4_piv['Constituency-code'].isin(df[(df['Party']==_FOURL[0]) & (df['vote%']<5)]['Constituency-code'].values)]


Out[24]:
Party Constituency-code Bharatiya Janata Party Indian National Congress Nationalist Congress Party Shivsena
83 84 6908 65079 1818 78520
118 119 9339 855 112787 66345
121 122 6996 43415 68284 55651
190 191 9452 60496 11387 44251
191 192 6054 45853 3500 60865
193 194 3066 73152 3258 94408
195 196 4615 2408 120235 62081
199 200 4260 94227 108400 2184
260 261 2102 7642 85595 104419
268 269 4819 60206 2692 70582
272 273 5521 1035 123626 117692
274 275 5258 107288 NaN 107998

In [25]:
temp=df.sort(['Constituency-code', 'Votes'], ascending=[1, 0]).groupby('Constituency-code', as_index=False).first()

In [26]:
temp[temp['Constituency-code'].isin(df[(df['Party']==_FOURL[0]) & (df['vote%']<5)]['Constituency-code'].values)]


Out[26]:
Constituency-code Candidate Party Votes Constituency Total-Votes vote%
83 84 ASHTIKAR PATIL NAGESH BAPURAO Shivsena 78520 Hadgaon 187343 41.91
118 119 CHHAGAN BHUJBAL Nationalist Congress Party 112787 Yevla 193831 58.19
121 122 ZIRWAL NARHARI SITARAM Nationalist Congress Party 68284 Dindori 201826 33.83
190 191 DHAIRYASHEEL MOHAN PATIL Peasants And Workers Party of India 64616 Pen 202437 31.92
191 192 SUBHASH ALIAS PANDITSHET PATIL Peasants And Workers Party of India 76959 Alibag 202416 38.02
193 194 GOGAWALE BHARAT MARUTI Shivsena 94408 Mahad 181112 52.13
195 196 DILIP DATTATRAY WALSE PATIL Nationalist Congress Party 120235 Ambegaon 193539 62.12
199 200 DATTATRAY VITHOBA BHARNE Nationalist Congress Party 108400 Indapur 218031 49.72
260 261 DESAI SHAMBHURAJ SHIVAJIRAO Shivsena 104419 Patan 206589 50.54
268 269 NAIK VAIBHAV VIJAY Shivsena 70582 Kudal 141066 50.03
272 273 MUSHRIF HASAN MIYALAL Nationalist Congress Party 123626 Kagal 251452 49.16
274 275 NARKE CHANDRADIP SHASHIKANT Shivsena 107998 Karvir 244072 44.25

In [26]: