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]:
In [9]:
df_old_4.head(2)
Out[9]:
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]:
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]:
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()
In [15]:
df_old_4_piv[:4]
Out[15]:
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]:
In [18]:
funcfour(df_4, df_old_4, 'SHS')
Out[18]:
In [19]:
funcfour(df_4, df_old_4, 'INC')
Out[19]:
In [20]:
funcfour(df_4, df_old_4, 'NCP')
Out[20]:
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]
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]:
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]:
In [26]: