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_Statement4.py
More file actions
40 lines (34 loc) · 1.99 KB
/
Copy pathProblem_Statement4.py
File metadata and controls
40 lines (34 loc) · 1.99 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
39
40
##########################Problem 4: Candidate Performance Based on Age##############################
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df=pd.read_csv(r"C:\Users\ayush\OneDrive\Desktop\PythonCA2\Data.csv")
###Objective 1: Determine the average age of winning candidates per s
# Convert age to
df['age'] = pd.to_numeric(df['age'], errors='coerce')
# Get only winning candidates (position = 1) and calculate the average age
winning_candidates = df[df['position'] == 1]
avg_winner_age_per_state = winning_candidates.groupby("state_name")["age"].mean()
print(avg_winner_age_per_state)
###Objective 2: Identify the youngest and oldest candidates who have won an election
youngest_winner = winning_candidates.loc[winning_candidates['age'].idxmin()]
oldest_winner = winning_candidates.loc[winning_candidates['age'].idxmax()]
print("Youngest Winning Candidate:", youngest_winner[['candidate_name', 'age', 'state_name', 'year']])
print("Oldest Winning Candidate:", oldest_winner[['candidate_name', 'age', 'state_name', 'year']])
###Objective 3: Find out if younger candidates have a higher or lower success rate
# Define a threshold for younger candidates (e.g., below median age)
median_age = df['age'].median()
young_candidates = df[df['age'] < median_age]
old_candidates = df[df['age'] >= median_age]
young_success_rate = (young_candidates[young_candidates['position'] == 1].shape[0]/young_candidates.shape[0])*100
old_success_rate = (old_candidates[old_candidates['position'] == 1].shape[0]/old_candidates.shape[0])*100
print(f"Success Rate of Younger Candidates: {young_success_rate:.2f}%")
print(f"Success Rate of Older Candidates: {old_success_rate:.2f}%")
###Objective 4: Analyze the distribution of ages among all candidates (Visualization)
plt.figure(figsize=(10, 6))
sns.histplot(df['age'], bins=20, kde=True, color="blue")
plt.xlabel("Candidate Age")
plt.ylabel("Frequency")
plt.title("Distribution of Candidate Ages")
plt.show()