Skip to content

Instantly share code, notes, and snippets.

@PatWg
Last active December 8, 2023 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatWg/d8915f2417ca1da58c892e089aebd643 to your computer and use it in GitHub Desktop.
Save PatWg/d8915f2417ca1da58c892e089aebd643 to your computer and use it in GitHub Desktop.
ICC23-24: Correction de la série 12
from plotnine import ggplot, aes, geom_line
import pandas as pd
from dataclasses import dataclass
from typing import List, Dict
@dataclass
class DataEntry:
gender: str
name: str
birth_year: int
count: int
with open('cours11-data/nat2018.csv', 'r', encoding='utf-8') as file:
lines: List[str] = file.read().splitlines()
lines = list(filter(lambda x: 'XXXX' not in x and '_PRENOMS_RARES' not in x, lines))
boys: List[str] = [DataEntry('M', line.split(';')[1], int(line.split(';')[2]), int(line.split(';')[3])) for line in lines if line.split(';')[0] == '1']
girls: List[str] = [DataEntry('F', line.split(';')[1], int(line.split(';')[2]), int(line.split(';')[3])) for line in lines if line.split(';')[0] == '2']
births_by_year: Dict[int, List[int]] = {boy.birth_year:[] for boy in boys}
for child in boys+girls:
births_by_year[child.birth_year].append(child.count)
births = list(map(lambda x: f"{x[0]}: {sum(x[1])}", births_by_year.items()))
years = []
births_count = []
for item in births:
year, birth_count = [int(x) for x in item.split(': ')]
years.append(year)
births_count.append(birth_count)
naissances_annees = {"Annee": years, "Naissances": births_count}
naissances_par_annees = pd.DataFrame(naissances_annees)
plot = ggplot(naissances_par_annees) + aes(x = "Annee", y = "Naissances") + geom_line()
print(plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment