0% found this document useful (0 votes)
5 views

Week 05

desacde

Uploaded by

belcherbrea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Week 05

desacde

Uploaded by

belcherbrea
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1

Week 05, Day 02 - "In-Class" (GROUP) Discussion - Data Visualization

Pathak, Anurag N

Atla, Rajeev R

Haque, Suhail
Nimmala, Thanvi

Gurbanov, Danny

We obtained our data from https://data.fivethirtyeight.com/

This dataset contains columns on political polling, specifically regarding different pollsters,
modes of polling, sample sizes, political affiliations, and results for Democratic, Republican, and
independent candidates.

We will create two visualizations from this dataset:


Bar chart showing the average support for Democratic and Republican candidates across all
polls.
Line chart showing the trend in support for Democratic and Republican candidates over time.

Bar chart

Python Code

import pandas as pd

# Load the CSV file


file_path = '/mnt/data/generic ballot polls.csv'

data = pd.read_csv(file_path)

# Display the first few rows of the dataset to understand its structure
2

data.head(), data.columns

Above is the first visualization—a bar chart of the average supports for the Democratic and
Republican candidates. It seems obvious from the visualization that the average support between
both Democrats and Republicans is almost equal.

Line chart

Below is a line chart which visualizes the trend in support for both parties over time. This chart
highlights the fluctuations in support for both parties as the election date approaches.

Python code

# Convert 'election_date' to datetime format


data['election_date'] = pd.to_datetime(data['election_date'], errors='coerce')

# Group by election date and calculate the mean support for each date

trend_data = data.groupby('election_date')[['dem', 'rep']].mean().dropna()

# Visualization 2: Line chart showing the trend in support for Democratic and Republican
candidates over time
3

plt.figure(figsize=(12, 8))

plt.plot(trend_data.index, trend_data['dem'], label='Democratic Support', color='blue',


marker='o')

plt.plot(trend_data.index, trend_data['rep'], label='Republican Support', color='red', marker='o')

plt.title('Trend in Support for Democratic and Republican Candidates Over Time')

plt.xlabel('Election Date')

plt.ylabel('Average Support (%)')


plt.xticks(rotation=45)

plt.legend()
plt.grid(True)

plt.show()

You might also like