3.2. This is my Jupyter Book!#
import urllib.request
import csv
import statistics
import random
from collections import defaultdict, Counter
import matplotlib.pyplot as plt
# Download the latest version of the CSV file from the URL and save it locally
url = 'https://raw.githubusercontent.com/quadriga-dk/OER-Workshop/refs/heads/main/assets/cleaned_trust_data.csv'
response = urllib.request.urlopen(url)
data = response.read().decode('utf-8')
with open('../assets/cleaned_trust_data.csv', 'w') as file:
file.write(data)
# Load the cleaned trust data from the downloaded CSV file
with open('../assets/cleaned_trust_data.csv', 'r') as trust_data_file:
survey_data = list(csv.DictReader(trust_data_file))
# Let's see what our cleaned data looks like
print("\n๐ฏ Our cleaned data structure:")
print("Each observation now has exactly 3 pieces of information:")
for i, observation in enumerate(survey_data[:5]):
print(f" {i+1}. {observation}")
# Convert Trust_Percentage from str to float for calculations
for obs in survey_data:
obs['Trust_Percentage'] = float(obs['Trust_Percentage'])
print("After transforming 'Trust_Percentage' to a floating point number:")
for i, observation in enumerate(survey_data[:5]):
print(f" {i+1}. {observation}")
print(f"\n๐ Dataset summary:")
countries = [obs['Country'] for obs in survey_data]
trust_values = [obs['Trust_Percentage'] for obs in survey_data]
years = [obs['Year'] for obs in survey_data if obs['Year']]
print(f" ๐ Countries covered: {len(set(countries))}")
print(f" ๐
Time period: {min(years) if years else 'N/A'} - {max(years) if years else 'N/A'}")
print(f" ๐ Trust range: {min(trust_values):.1f}% to {max(trust_values):.1f}%")
๐ฏ Our cleaned data structure:
Each observation now has exactly 3 pieces of information:
1. {'Country': 'Albania', 'Year': '2004', 'Trust_Percentage': '23.2'}
2. {'Country': 'Albania', 'Year': '2010', 'Trust_Percentage': '9.221174'}
3. {'Country': 'Albania', 'Year': '2022', 'Trust_Percentage': '2.77237'}
4. {'Country': 'Algeria', 'Year': '2004', 'Trust_Percentage': '10.76443'}
5. {'Country': 'Algeria', 'Year': '2014', 'Trust_Percentage': '17.16667'}
After transforming 'Trust_Percentage' to a floating point number:
1. {'Country': 'Albania', 'Year': '2004', 'Trust_Percentage': 23.2}
2. {'Country': 'Albania', 'Year': '2010', 'Trust_Percentage': 9.221174}
3. {'Country': 'Albania', 'Year': '2022', 'Trust_Percentage': 2.77237}
4. {'Country': 'Algeria', 'Year': '2004', 'Trust_Percentage': 10.76443}
5. {'Country': 'Algeria', 'Year': '2014', 'Trust_Percentage': 17.16667}
๐ Dataset summary:
๐ Countries covered: 116
๐
Time period: 2004 - 2022
๐ Trust range: 2.1% to 74.9%