Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Trabajo_grupal/WG3/Grupo_9_py_Ej1.ipynb
2714 views
Kernel: Python 3 (ipykernel)

Ej.1

from IPython.display import display, HTML display(HTML(data=""" <style> div#notebook-container { width: 75%; } div#menubar-container { width: 80%; } div#maintoolbar-container { width: 80%; }a </style> """))
''' Importar librerias - numpy - pandas - Series ''' import numpy as np import pandas as pd from pandas import DataFrame,Series
junin = pd.read_excel("../../data/Region_Junin.xlsx") junin.head(3)
# 0. Dropear primera columna dado que es una repiticion del indice junin.drop(junin.columns[0], axis=1, inplace=True) junin.head(3)
# Pregunta 1: Obtener el nombre de todas las variables list(junin.columns) #nombres
['District', 'Place', '4_6_years_men', '4_6_years_women', '4_6_years_total', '6_14_years_men', '6_14_years_women', '6_14_years_total', 'man_read', 'women_read', 'total_read', 'men_not_read', 'women_not_read', 'total_not_read', 'man_write', 'women_write', 'total_write', 'men_not_write', 'women_not_write', 'total_not_write', 'instruction_men', 'instruction_women', 'instruction_total', 'no_instruction_men', 'no_instruction_women', 'no_instruction_total', 'finished_instr_men', 'finished_instr_women', 'finished_instr_total', 'not_finished_instr_men', 'not_finished_instr_women', 'not_finished_instr_total', 'peruvian_men', 'peruvian_women', 'foreign_men', 'foreign_women', 'whites', 'natives', 'mestizos', 'blacks']
type(junin.columns) # Show the type
pandas.core.indexes.base.Index
# Pregunta 2: Mostrar el tipo de variables (type) así como presentar los principales estadísticos. junin.info() #se evidencia que la mayoría de variables son numeros (int, float)
<class 'pandas.core.frame.DataFrame'> RangeIndex: 197 entries, 0 to 196 Data columns (total 40 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 District 197 non-null object 1 Place 186 non-null object 2 4_6_years_men 197 non-null int64 3 4_6_years_women 197 non-null int64 4 4_6_years_total 197 non-null int64 5 6_14_years_men 196 non-null float64 6 6_14_years_women 197 non-null int64 7 6_14_years_total 197 non-null int64 8 man_read 197 non-null int64 9 women_read 193 non-null float64 10 total_read 197 non-null int64 11 men_not_read 197 non-null int64 12 women_not_read 197 non-null int64 13 total_not_read 196 non-null float64 14 man_write 196 non-null float64 15 women_write 196 non-null float64 16 total_write 192 non-null float64 17 men_not_write 191 non-null float64 18 women_not_write 197 non-null int64 19 total_not_write 194 non-null float64 20 instruction_men 197 non-null int64 21 instruction_women 195 non-null float64 22 instruction_total 195 non-null float64 23 no_instruction_men 194 non-null float64 24 no_instruction_women 197 non-null int64 25 no_instruction_total 197 non-null int64 26 finished_instr_men 194 non-null float64 27 finished_instr_women 192 non-null float64 28 finished_instr_total 196 non-null float64 29 not_finished_instr_men 196 non-null float64 30 not_finished_instr_women 196 non-null float64 31 not_finished_instr_total 196 non-null float64 32 peruvian_men 196 non-null float64 33 peruvian_women 196 non-null float64 34 foreign_men 196 non-null float64 35 foreign_women 195 non-null float64 36 whites 196 non-null float64 37 natives 191 non-null float64 38 mestizos 195 non-null float64 39 blacks 197 non-null int64 dtypes: float64(24), int64(14), object(2) memory usage: 61.7+ KB
junin.describe() # Principales estadísticos #en promedio hay 17=16.97 aprx. niños de 4 a 6 años en cada lugar #con una desviacion de 22 +/- #el máximo de niños de 4 a 6 años en un lugar es de 184
# Pregunta 3: Verifique si las columnas presentan missing values junin.isnull().sum() #verificar con funcion isna # se evidencia que hay missings en las columnas; Place, total_not_read, man_write, entre otras.
District 0 Place 11 4_6_years_men 0 4_6_years_women 0 4_6_years_total 0 6_14_years_men 1 6_14_years_women 0 6_14_years_total 0 man_read 0 women_read 4 total_read 0 men_not_read 0 women_not_read 0 total_not_read 1 man_write 1 women_write 1 total_write 5 men_not_write 6 women_not_write 0 total_not_write 3 instruction_men 0 instruction_women 2 instruction_total 2 no_instruction_men 3 no_instruction_women 0 no_instruction_total 0 finished_instr_men 3 finished_instr_women 5 finished_instr_total 1 not_finished_instr_men 1 not_finished_instr_women 1 not_finished_instr_total 1 peruvian_men 1 peruvian_women 1 foreign_men 1 foreign_women 2 whites 1 natives 6 mestizos 2 blacks 0 dtype: int64
junin.isnull().sum() #verificar con funcion isnull (mismo resultado)
District 0 Place 11 4_6_years_men 0 4_6_years_women 0 4_6_years_total 0 6_14_years_men 1 6_14_years_women 0 6_14_years_total 0 man_read 0 women_read 4 total_read 0 men_not_read 0 women_not_read 0 total_not_read 1 man_write 1 women_write 1 total_write 5 men_not_write 6 women_not_write 0 total_not_write 3 instruction_men 0 instruction_women 2 instruction_total 2 no_instruction_men 3 no_instruction_women 0 no_instruction_total 0 finished_instr_men 3 finished_instr_women 5 finished_instr_total 1 not_finished_instr_men 1 not_finished_instr_women 1 not_finished_instr_total 1 peruvian_men 1 peruvian_women 1 foreign_men 1 foreign_women 2 whites 1 natives 6 mestizos 2 blacks 0 dtype: int64
# Pregunta 4: Cambie el nombre de las siguientes variables: #Place : comunidad, men_not_read: nlee_hombre, women_not_read: nlee_mujer, total_not_read: ntotal_lee, men_not_write: nesc_hombre, women_not_write: nesc_hombre, total_not_write: nesc_total. junin.rename(columns = {'Place':'comunidad', 'men_not_read':'homxlee', 'women_not_read':'mujerxlee', 'total_not_read':'totalxlee'}, inplace = True) junin.head(5)
# Pregunta 5: Muestre los valores únicos de las siguientes variables (comunidad , District) # Comunidad: print(junin.comunidad.unique()) print(len(junin.comunidad.unique())) #hay 183 valores unicos de comunidad
['C.CIUDAD DEL CERRO' 'P.YANACANCHA' 'VICO' 'RANCA' 'PASCO' nan 'CAJAMARQUILLA' 'CAICO' 'ROCO' 'P.QUINUA' 'H.PARIAHUANCA' 'P.SULLUMARCA' 'H.SAN ANDRÉS' 'P.HUALLAY' 'C.HUAYCHAO' 'COCHAMARCA' 'P.CHACAYÁN' 'VILCABAMBA' 'CUCHES' 'CHANGO' 'MITO' 'ANTAPIRCA' 'P.CAYNA' 'A. COLPAS' 'C.MASQUIN' 'P.YAMOR' 'TANGOR' 'QUIO' 'CAURY' 'CHUCCHUC' 'COQUIN' 'A.YAPOC' 'P.YANAHUANCA' 'HUAYLACIRCA' 'HUARANTAMBO' 'ANDUCHACA' 'VILLO' 'MICHIVILCA' 'PILLAO' 'YACÁN' 'TÁPUC' 'CHAUPIMARCA' 'P.OXAPAMPA' 'P.JAUJA' 'A.CHUNÁN' 'C.PACAPACHA' 'HUANCAS' 'A.HUERTAS' 'PACA' 'MASMA' 'ATAURA' 'HUALÁ' 'JULCÁN' 'P.RICRÁN' 'A.PAUCÁN' 'P.APATA' 'HUAMALÍ' 'A.SAN LORENZO' 'PACUCHO' 'P.ORCOTUNA' 'A.VICSO' 'TISTES' 'P.HUARIPAMPA' 'A.PARCO' 'PACCHA' 'CANCHAPUNCO' 'C.ULLUSCA' 'CANCHAS' 'P.MUQUIYAUYO' 'MUQUI' 'P.ACOLLA' 'MARCO' 'TRAGADERO' 'ACAYA' 'YAUJAILLO' 'CURICACA' 'C.LLACUARI' 'P.HUANCARÍ' 'C.CHALHUAS' 'ARAMACHAY' 'P.MITO' 'A.CHAMBARÁ' 'C.QUICHA' 'HUACHAC' 'A.ACO' 'P.LLOCLLAPAMPA' 'VISCAS ESPERANZA' 'CONCHAILLO' 'MATA-CHICO' 'MATA-GRANDE' 'P.CONCEPCIÓN' 'SANTA ROSA DE OCOPA' 'HUANTAR' 'A.ALAYO' 'C.SAN ANTONIO DE CARICANCHO' 'SANTO DOMINGO' 'P.CÓMAS' 'C.CHUPA' 'COCHAS' 'P.MATAHUASI' 'P.HUANCAYO' 'CAJAS' 'P.CHUPACA' 'A.ISCOS' 'ANTAPAMPA' 'HUAUSCA GRANDE' 'SAN JUAN DE TARPA' 'COPCA' 'HUAYAN' 'HUAMANCACA' 'PILCOMAYO' 'AHUAC' 'ACAC' 'P.SAN GERÓNIMO' 'HUALHUAS' 'SAN PEDRO DE SAÑO' 'QUICHUAY' 'CASACANCHA' 'A.INGENIO' 'P.SANTA CRUZ DE QUILCAY' 'RANGRA' 'P.SAPALLANGA' 'A.LA PUNTA' 'P.PUCARÁ' 'C.PATALÁ' 'MARCAVALLE' 'COCHARCAS' 'P.CHONGOS' 'C.TURPAC Y TINYARI' 'SOCOS' 'PUMPUNGA' 'CHUPURO' 'CARHUAPACHA' 'A.HUAMANCACA' 'P.SICAYA' 'A.HUANCHAC' 'C.CACHICAMARCA' 'P.COLCA' 'A.CHACAPAMPA' 'HUASICANCHA' 'CUCHO' 'CHILIAY' 'POTACA' 'LARIA' 'CHONGOS ALTO' 'P.HUAYUCACHI' 'HUAMANMARCA' 'CACAS' 'HUARAPUQUIO' 'CULLHUAS' 'RETAMA' 'PACHA' 'P.TARMA' 'MACO' 'MULLUERO' 'CHANCHA' 'TARMATAMBO' 'P.JUNÍN' 'HUASAHUASY' 'PARI' 'YANEC' 'CHUPÁN' 'ACONCOCHA' 'ONDORES' 'P.ACOBAMBA' 'PICOY' 'C.HUARACAYO' 'A.PALCA' 'C.HUAILAHUICHÁN' 'P.PALCAMAYO' 'P.OROYA' 'HUAYNACANCHA' 'HUAYHUAY' 'P.CARHUAMAYO' 'ULCUMAYO' 'P.SAN RAMÓN' 'P.VISCATÁN' 'PAN DE AZÚCAR' 'SUITUCANCHA' 'CHACAPALCA' 'P.POMACOCHA' 'YAULI' 'MINAS DE MOROCOCHA'] 183
#District: print(junin.District.unique()) print(len(junin.District.unique())) #hay 35 valores unicos de comunidad
['CIUDAD DEL CERRO' 'HUAYLLAY' 'CHACAYÁN' 'CAYNA' 'YANAHUANCA' 'NINACACA' 'JAUJA' 'APATA' 'ORCOTUNA' 'HUARIPAMPA' 'MUQUIYAUYO' 'ACOLLA' 'CINCOS' 'MITO' 'LLOCLLAPAMPA' 'CONCEPCIÓN' 'COCHAS' 'MATAHUASI' 'HUANCAYO' 'CHUPACA' 'SAN GERÓNIMO' 'SAPALLANGA' 'CHONGOS' 'SICAYA' 'COLCA' 'HUAYUCACHI' 'TARMA' 'JUNÍN' 'ACOBAMBA' 'OROYA' 'CARHUAMAYO' 'CHANCHAMAYO' 'VITOC' 'CHACAPALCA' 'YAULI'] 35
# Pregunta 6: Crear columnas con las siguiente información: # a. % de mujeres que no escriben ni leen, # b. % de varones que no escriben ni leen, # c. % de nativos respecto al total de la población. junin['Total_poblacion']=junin['peruvian_men']+junin['peruvian_women']+junin['foreign_men']+junin['foreign_women']
#a. junin['%_women_nread_nwrite'] = junin['mujerxlee'] * 100 / junin['totalxlee']
#b. junin['%_men_nread_nwrite'] = junin['homxlee'] * 100 / junin['totalxlee']
#c. junin['%_natives'] = junin['natives'] * 100 / junin['Total_poblacion']
junin.head(3)
# Pregunta 7: Crear una base de datos con la siguiente información: # a. Quedarse con la información de los distritos de Ciudad del Cerro, Jauja, Acolla, San Gerónimo, Tarma, Oroya y Concepción junin1 = junin[junin["District"].isin(["CIUDAD DEL CERRO","JAUJA","ACOLLA","SAN GERÓNIMO", "TARMA","OROYA","CONCEPCIÓN"])] junin1
# b. Quedarse con las comunidades que cuentan con nativos (implica > 0) y mestizos (implica > 0) junin1=junin1[(junin1.natives > 0 ) & (junin1.mestizos > 0)] junin1.head(3)
#c. Quedarse con las variables trabajadas en el punto 6), nombre de distrito y comunidad. junin1 = junin1[['District','comunidad','%_women_nread_nwrite','%_men_nread_nwrite','Total_poblacion']] junin1
# d. Guardar la base de datos en formato csv en la carpeta data. Base_cleaned_WG(numero de grupo) #csv junin1.to_csv("../../data/Base_cleaned_WG9.csv") #excel junin1.to_excel("../../data/Base_cleaned_WG9.xlsx")