-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #616 from basedosdados/add-taxa-alfabetizacao-saeb
add: `br_inep_saeb.{brasil,uf}_taxa_alfabetizacao`
- Loading branch information
Showing
4 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
models/br_inep_saeb/br_inep_saeb__brasil_taxa_alfabetizacao.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{{ | ||
config( | ||
alias="brasil_taxa_alfabetizacao", | ||
schema="br_inep_saeb", | ||
materialized="table", | ||
) | ||
}} | ||
|
||
select | ||
safe_cast(ano as int64) ano, | ||
safe_cast(rede as string) rede, | ||
safe_cast(localizacao as string) localizacao, | ||
safe_cast(area as string) area, | ||
safe_cast(taxa_alfabetizacao as float64) taxa_alfabetizacao, | ||
from `basedosdados-staging.br_inep_saeb_staging.brasil_taxa_alfabetizacao` as t |
13 changes: 13 additions & 0 deletions
13
models/br_inep_saeb/br_inep_saeb__uf_taxa_alfabetizacao.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{{ | ||
config( | ||
alias="uf_taxa_alfabetizacao", schema="br_inep_saeb", materialized="table" | ||
) | ||
}} | ||
select | ||
safe_cast(ano as int64) ano, | ||
safe_cast(sigla_uf as string) sigla_uf, | ||
safe_cast(rede as string) rede, | ||
safe_cast(localizacao as string) localizacao, | ||
safe_cast(area as string) area, | ||
safe_cast(taxa_alfabetizacao as float64) taxa_alfabetizacao, | ||
from `basedosdados-staging.br_inep_saeb_staging.uf_taxa_alfabetizacao` as t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import pandas as pd | ||
import basedosdados as bd | ||
import os | ||
|
||
os.getcwd() | ||
|
||
INPUT = os.path.join(os.getcwd(), "input") | ||
OUTPUT = os.path.join(os.getcwd(), "output") | ||
URL = "https://download.inep.gov.br/saeb/resultados/saeb_2021_brasil_estados_municipios_c_tx_alfabetizado.xlsx" | ||
|
||
os.makedirs(INPUT, exist_ok=True) | ||
os.makedirs(OUTPUT, exist_ok=True) | ||
|
||
os.system(f"cd {INPUT}; curl -O -k {URL}") | ||
|
||
# Brasil | ||
|
||
df_br = pd.read_excel(os.path.join(INPUT, os.path.basename(URL)), sheet_name="Brasil") | ||
|
||
df_br.columns | ||
|
||
df_br["ID"].unique() | ||
|
||
df_br["ANO_SAEB"].unique() | ||
|
||
df_br["DEPENDENCIA_ADM"].unique() | ||
|
||
df_br["LOCALIZACAO"].unique() | ||
|
||
df_br["CAPITAL"].unique() | ||
|
||
df_br["TX_ALFABETIZADO"].unique() | ||
|
||
df_br["DEPENDENCIA_ADM"] = df_br["DEPENDENCIA_ADM"].str.lower() | ||
|
||
df_br["LOCALIZACAO"] = df_br["LOCALIZACAO"].str.lower() | ||
|
||
df_br["CAPITAL"] = df_br["CAPITAL"].str.lower() | ||
|
||
df_br = df_br.drop(columns=["ID", "MEDIA_2_LP", "MEDIA_2_MT"]) | ||
|
||
df_br = df_br.rename( | ||
columns={ | ||
"ANO_SAEB": "ano", | ||
"DEPENDENCIA_ADM": "rede", | ||
"LOCALIZACAO": "localizacao", | ||
"CAPITAL": "area", | ||
"TX_ALFABETIZADO": "taxa_alfabetizacao", | ||
}, | ||
errors="raise", | ||
) | ||
|
||
df_br.to_csv(os.path.join(OUTPUT, "brasil_taxa_alfabetizacao.csv"), index=False) | ||
|
||
# Estados | ||
|
||
df_ufs = pd.read_excel(os.path.join(INPUT, os.path.basename(URL)), sheet_name="Estados") | ||
|
||
df_ufs["DEPENDENCIA_ADM"] = df_ufs["DEPENDENCIA_ADM"].str.lower() | ||
|
||
df_ufs["LOCALIZACAO"] = df_ufs["LOCALIZACAO"].str.lower() | ||
|
||
df_ufs["CAPITAL"] = df_ufs["CAPITAL"].str.lower() | ||
|
||
bd_dirs_ufs = bd.read_sql( | ||
"select sigla, nome from `basedosdados.br_bd_diretorios_brasil.uf`", | ||
billing_project_id="basedosdados-dev", | ||
) | ||
|
||
uf_map = dict([(i["nome"], i["sigla"]) for i in bd_dirs_ufs.to_dict("records")]) # type: ignore | ||
|
||
df_ufs["NO_UF"].unique() | ||
|
||
df_ufs["NO_UF"] = df_ufs["NO_UF"].replace(uf_map) | ||
|
||
df_ufs["NO_UF"].isna().sum() | ||
|
||
df_ufs = df_ufs.drop(columns=["CO_UF", "MEDIA_2_LP", "MEDIA_2_MT"]) | ||
|
||
df_ufs = df_ufs.rename( | ||
columns={ | ||
"ANO_SAEB": "ano", | ||
"NO_UF": "sigla_uf", | ||
"DEPENDENCIA_ADM": "rede", | ||
"LOCALIZACAO": "localizacao", | ||
"CAPITAL": "area", | ||
"TX_ALFABETIZADO": "taxa_alfabetizacao", | ||
}, | ||
errors="raise", | ||
) | ||
|
||
df_ufs.to_csv(os.path.join(OUTPUT, "uf_taxa_alfabetizacao.csv"), index=False) | ||
|
||
# Upload | ||
|
||
## Brasil | ||
|
||
tb_br = bd.Table(dataset_id="br_inep_saeb", table_id="brasil_taxa_alfabetizacao") | ||
tb_br.create(os.path.join(OUTPUT, "brasil_taxa_alfabetizacao.csv")) | ||
|
||
|
||
tb_uf = bd.Table(dataset_id="br_inep_saeb", table_id="uf_taxa_alfabetizacao") | ||
tb_uf.create(os.path.join(OUTPUT, "uf_taxa_alfabetizacao.csv")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters