forked from ayush-4299/Election-Data-Analysis-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_Statement3.py
More file actions
38 lines (31 loc) · 1.94 KB
/
Copy pathProblem_Statement3.py
File metadata and controls
38 lines (31 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#############################Problem 3:Election Competitiveness Analysis#############################################
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Load the dataset
df=pd.read_csv(r"C:\Users\ayush\OneDrive\Desktop\PythonCA2\Data.csv")
###Objective 1: Find the percentage of elections where the winning margin was below 5%
# Convert margin percentage to numeric
df['margin_percentage'] = pd.to_numeric(df['margin_percentage'], errors='coerce')
# Calculate percentage of elections where margin was below 5%
close_elections = df[df['margin_percentage'] < 5].shape[0]
total_elections = df.shape[0]
close_elections_percentage = (close_elections / total_elections) * 100
print(f"Percentage of elections with a winning margin below 5%: {close_elections_percentage:.2f}%")
###Objective 2: Identify constituencies with the closest competition (smallest margin percentage)
# Find the constituency with the smallest margin percentage
closest_competition = df[df['margin_percentage'] == df['margin_percentage'].min()]
print("Constituency with the closest competition:")
print(closest_competition[['ac_name', 'state_name', 'year', 'margin_percentage']])
###Objective 3: Compute the average number of candidates contesting in a constituency per election
# Compute average number of candidates per constituency per election
avg_candidates_per_constituency = df.groupby(['year', 'ac_name'])['ac_total_candidates'].mean().mean()
print(f"Average number of candidates per constituency per election: {avg_candidates_per_constituency:.2f}")
###Objective 4: Analyze the effect of multi-candidate races on vote share distribution(Visualization)
plt.figure(figsize=(10, 6))
sns.scatterplot(x=df['ac_total_candidates'], y=df['vote_share_percentage'], alpha=0.5)
plt.xlabel("Number of Candidates")
plt.ylabel("Vote Share Percentage")
plt.title("Effect of Multi-Candidate Races on Vote Share")
plt.show()