<jupyter_start><jupyter_text>Strength of a partyStrength of a party lies in converting voters into votes.  To understand it, computed numbers votes for which a party contested and how much they gained.<jupyter_code>import pandas as pd

import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
%matplotlib inline
plt.style.use('ggplot')
indata = pd.read_csv("Data/Tamil Nadu.csv")
indata.head()
consta_votes = indata.groupby(['cname']).agg(votes = ('Total Votes', 'sum'))
consta_votes.reset_index(inplace=True)
party_votes = indata.loc[indata['Party'] != 'Independent'].groupby(['Party']).agg(TotalVotes=('Total Votes', 'sum'))
party_votes.sort_values(['TotalVotes', 'Party'], ascending=False, inplace=True)
party_votes.reset_index(inplace=True)
party_votes['CompetedVotes'] = 0
party_votes['CompetedConsta'] = 0
for idx, row in party_votes.iterrows():
    contested = indata.loc[indata['Party'] == row['Party']]['cname'].unique()
    votes = consta_votes.loc[consta_votes['cname'].isin(contested)]['votes'].sum()
    party_votes.loc[idx, 'CompetedVotes'] = votes
    party_votes.loc[idx, 'CompetedConsta'] = len(contested)
party_votes['PctVotes'] = party_votes['TotalVotes'] / party_votes['CompetedVotes']
party_votes.sort_values(['PctVotes'], ascending=False, inplace=True)
party_votes.head(12)
plot_data = party_votes.iloc[:12]
fig, ax = plt.subplots(figsize=(16, 9))
ax.bar(plot_data['Party'], plot_data['PctVotes'], color="C0")


ax.tick_params(axis="y", colors="C0")
ax.set_ylabel('Percentage', fontdict={'fontsize': 15, 'fontweight': 'medium'})
ax.tick_params(axis="x", labelrotation=90)
ax.set_title("Vote Gained / Competed", 
             fontdict={'fontsize': 20, 'fontweight': 'medium'})

for p in ax.patches:
    ax.annotate(str(round(p.get_height(), 2)), (p.get_x() + 0.25, p.get_height() + 0.005))
    
plt.show()<jupyter_output><empty_output><jupyter_text>Observations- These are the top 12 parties in TN out of the 108 parties competed in this election excluding the independent candidates.- **<NAME>** DMK is really strong, they competed in 188 Constituency and gained 46% of votes from there- **Communist Party of India, <NAME>** gained 40+% of votes, we cannot call them really strong because they competed in very few constituency. It doesn't mean if they compete in 100+ constituency they will win similar score. Their strength lies in picking the constituency where they are likely to win- **Indian National Congress**, have scope for improvement, They competed in 25 place and won 40+% votes, they could compete in few more place and win similar score- **<NAME>, <NAME> & <NAME>** competed in 234, 180 & 165 constituency respectively. Even though they have lot of votes but the win rate is extremely low. It shows there presence is not felt in any part of Tamil Nadu. Long way for them to go.    - Probably they should gain confidence from few constituency and then focus on rest of the Tamil Nadu  Comepted vs Won<jupyter_code>win_count = indata.sort_values('Total Votes', ascending=False).drop_duplicates(['cname'])['Party'].value_counts()
win_count = win_count.reset_index()
win_count.rename({'index':'Party', 'Party':'Won'}, axis='columns', inplace=True)
party_votes_won = party_votes.merge(win_count, how='left', on='Party')
party_votes_won.dropna(subset=['Won'], inplace=True)
party_votes_won['WonRatio'] = party_votes_won['Won'] / party_votes_won['CompetedConsta']
party_votes_won.sort_values(['WonRatio', 'TotalVotes'], ascending=False)<jupyter_output><empty_output><jupyter_text>Share of Constituency Won<jupyter_code>party_votes_won.set_index(['Party'], inplace=True)
party_votes_won.sort_values(['Won'], ascending=False, inplace=True)
party_votes_won.plot(kind='pie', y='Won', x='Party', figsize=(8, 8), legend=False, title='No. Constituency Won', 
                    autopct='%1.1f%%', subplots=True)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13955874807593638, 'fraction_numerical': 0.014622883530015392, 'mean_word_length': 6.615234375, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '273268', 'license': 'Apache-2.0', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1273, 'n_words': 344, 'path': 'TN Votes Gained Vs Competed.ipynb', 'repo_name': 'satcos/2021electionresult', 'size': '3990'}