updated = updated.melt(id_vars=["data"], var_name="Concelho", value_name="Casos") updated = updated.merge( population[["Concelho", "2019"]], how="left", left_on="Concelho", right_on="Concelho", ) updated.fillna(0, inplace=True) updated["ratio"] = round( updated["Casos"] * 100 * 1000 / updated["2019"], 1) updated = updated.pivot_table(values="ratio", index="data", columns="Concelho") updated = updated.reset_index(level=0) cols = [x for x in updated.columns if x != "data"] updated[cols] = updated[cols].diff(2) # 14 days updated = updated[2:] func = convert_to_int if i == DATA_CONCELHOS_14DIAS_CSV else convert_to_float updated = convert(updated, cols, func) # sort by date updated = updated.sort_values("data") # convert back into dd-mm-yyyy updated["data"] = updated["data"].dt.strftime("%d-%m-%Y") updated.to_csv(i, index=False, line_terminator="\n")
Path(__file__).resolve().parents[2] / "data_concelhos_incidencia.csv") PATH_TO_CSV_14DIAS = str( Path(__file__).resolve().parents[2] / "data_concelhos_14dias.csv") # Get list of municipalities concelhos_df = get_list_municipalities() # Get list of cases casos_df = get_list_cases_long() casos_df["confirmados_14"] = (casos_df["incidencia"] * casos_df["population"] / 100000.0) casos_df["confirmados_1"] = casos_df["confirmados_14"].div(14) cols = ["confirmados_14", "confirmados_1"] casos_df = convert(casos_df, cols, convert_to_int) cols = ["incidencia"] casos_df = convert(casos_df, cols, convert_to_float) cols = [x for x in casos_df.columns if x.startswith("densidade")] casos_df = convert(casos_df, cols, convert_to_float) cols = [x for x in casos_df.columns if x.startswith("population")] casos_df = convert(casos_df, cols, convert_to_int) cols = list(casos_df.columns) for i in ["data", "confirmados_14", "confirmados_1"]: cols.remove(i) cols.insert(cols.index("concelho") + 1, "confirmados_14") cols.insert(cols.index("concelho") + 2, "confirmados_1") cols.insert(0, "data") casos_df = casos_df[cols]
casos_df = get_list_cases_long() # Merge list of cases with list of municipalities casos_df = concelhos_df.merge(casos_df, how="left", on="concelho") # Helper for pivot table casos_df.loc[casos_df.data.isna(), ["confirmados"]] = -1 casos_df.loc[casos_df.data.isna(), ["data"]] = "24-03-2020" casos_df = casos_df.sort_values(by=["concelho"]) # Convert long table to wide table casos_wide = pd.pivot_table(casos_df, values="confirmados", index="data", columns="concelho") casos_wide = casos_wide.reset_index() casos_wide.data = pd.to_datetime(casos_wide.data, format="%d-%m-%Y") casos_wide = casos_wide.sort_values(by="data").reset_index(drop=True) casos_wide = casos_wide.replace(-1, np.nan) casos_wide = patch_concelhos1(casos_wide) casos_wide.data = casos_wide["data"].dt.strftime("%d-%m-%Y") casos_wide = patch_concelhos2(casos_wide) cols = [x for x in casos_wide.columns if not x.startswith("data")] casos_wide = convert(casos_wide, cols, convert_to_int) casos_wide.to_csv(PATH_TO_CSV, index=False, sep=",")