Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UP my solution #1

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
73 changes: 55 additions & 18 deletions pandas_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
To do that, you will load the data as pandas.DataFrame, merge the info and
aggregate them by regions and finally plot them on a map using `geopandas`.
"""

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt


def load_data():
"""Load data from the CSV files referundum/regions/departments."""
referendum = pd.DataFrame({})
regions = pd.DataFrame({})
departments = pd.DataFrame({})

referendum = pd.read_csv("data/referendum.csv", sep=";")
regions = pd.read_csv("data/regions.csv", sep=",")
departments = pd.read_csv("data/departments.csv", sep=",")
return referendum, regions, departments


Expand All @@ -28,8 +28,15 @@ def merge_regions_and_departments(regions, departments):
The columns in the final DataFrame should be:
['code_reg', 'name_reg', 'code_dep', 'name_dep']
"""

return pd.DataFrame({})
merged_df = pd.merge(
departments,
regions,
left_on="region_code",
right_on="code",
suffixes=("_dep", "_reg"),
)
result = merged_df[["code_reg", "name_reg", "code_dep", "name_dep"]]
return result


def merge_referendum_and_areas(referendum, regions_and_departments):
Expand All @@ -38,8 +45,17 @@ def merge_referendum_and_areas(referendum, regions_and_departments):
You can drop the lines relative to DOM-TOM-COM departments, and the
french living abroad.
"""

return pd.DataFrame({})
referendum = referendum[~referendum["Department code"].str.startswith("Z")]
referendum.loc[:, "Department code"] = (
referendum["Department code"].astype(str).str.zfill(2)
)
merged_df = pd.merge(
referendum,
regions_and_departments,
left_on="Department code",
right_on="code_dep",
)
return merged_df


def compute_referendum_result_by_regions(referendum_and_areas):
Expand All @@ -48,8 +64,17 @@ def compute_referendum_result_by_regions(referendum_and_areas):
The return DataFrame should be indexed by `code_reg` and have columns:
['name_reg', 'Registered', 'Abstentions', 'Null', 'Choice A', 'Choice B']
"""

return pd.DataFrame({})
region_results = referendum_and_areas.groupby("code_reg").agg(
{
"name_reg": "first",
"Registered": "sum",
"Abstentions": "sum",
"Null": "sum",
"Choice A": "sum",
"Choice B": "sum",
}
)
return region_results


def plot_referendum_map(referendum_result_by_regions):
Expand All @@ -61,23 +86,35 @@ def plot_referendum_map(referendum_result_by_regions):
should display the rate of 'Choice A' over all expressed ballots.
* Return a gpd.GeoDataFrame with a column 'ratio' containing the results.
"""

return gpd.GeoDataFrame({})
gdf = gpd.read_file(r"data/regions.geojson")
merged = gdf.merge(
referendum_result_by_regions, left_on="code", right_on="code_reg"
)
merged["expressed_ballots"] = (
merged["Registered"] - merged["Abstentions"] - merged["Null"]
)
merged["ratio"] = merged["Choice A"] / merged["expressed_ballots"]
ax = merged.plot(
column="ratio",
cmap="coolwarm",
legend=True,
figsize=(10, 10),
legend_kwds={"label": "Choice A (%)"},
)
ax.set_title("Referendum result by region")
return merged


if __name__ == "__main__":

referendum, df_reg, df_dep = load_data()
regions_and_departments = merge_regions_and_departments(
df_reg, df_dep
)
regions_and_departments = merge_regions_and_departments(df_reg, df_dep)
referendum_and_areas = merge_referendum_and_areas(
referendum, regions_and_departments
)

referendum_results = compute_referendum_result_by_regions(
referendum_and_areas
)
print(referendum_results)
referendum_and_areas)

plot_referendum_map(referendum_results)
plt.show()
Loading