Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ejercicios-clase-02-datos
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Carla Elena Gomez Alvarado
ejercicios-clase-02-datos
Commits
1d3334a7
Commit
1d3334a7
authored
4 years ago
by
Carla Elena Gomez Alvarado
Browse files
Options
Downloads
Patches
Plain Diff
Ejercicio importado del Jupyter Hub
parent
284851ea
Branches
master
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Ejercicio3.ipynb
+153
-0
153 additions, 0 deletions
Ejercicio3.ipynb
with
153 additions
and
0 deletions
Ejercicio3.ipynb
0 → 100644
+
153
−
0
View file @
1d3334a7
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#importar librerias a usar \n",
"\n",
"import csv, json\n",
"\n",
"#En esta variable se guardan todas las filas del CSV\n",
"compas = {}\n",
"\n",
"#previamente he cargado el archivo cega_tarea_2.csv en el directorio del pwd \n",
"\n",
"FILE_NAME = 'cega_tarea_2.csv'\n",
"\n",
"#Indices para cada columna\n",
"USERNAME = 0\n",
"NAME = 1\n",
"LASTNAME = 2\n",
"AGE = 3\n",
"COUNTRY = 4\n",
"CITY = 5\n",
"FIELD = 6\n",
"INSTITUTION = 7\n",
"HOBBY = 8\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#Funcion para leer un archivo\n",
"def read_file():\n",
" #Abre el archivo .csv\n",
" with open(FILE_NAME) as csv_file:\n",
" csv_reader = csv.reader(csv_file, delimiter=',')\n",
" #Saltarse los encabezados de las columnas\n",
" header = True\n",
" #Para cada fila del archivo .csv\n",
" for row in csv_reader:\n",
" if not header:\n",
" compas[row[USERNAME]] = {\n",
" \"name\" : row[NAME],\n",
" \"lastname\" : row[LASTNAME],\n",
" \"age\" : row[AGE],\n",
" \"country\" : row[COUNTRY],\n",
" \"city\" : row[CITY],\n",
" \"field\" : row[FIELD],\n",
" \"institution\" : row[INSTITUTION],\n",
" \"hobby\" : row[HOBBY],\n",
" }\n",
" else:\n",
" header = False"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Funcion principal\n",
"def main():\n",
" read_file()\n",
" print('--------------------------')\n",
" get_info_by_country('Colombia')\n",
" print('--------------------------')\n",
" print(f'Age avg: {get_age_avg()}')\n",
" print('--------------------------')\n",
" print(f'Institutions: {get_institutions()}')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def get_info_by_country(country):\n",
" #print con formato\n",
" print (\"{:<15} {:<20} {:<10} {:<10} {:<15} {:<25} {:<40} {:<10}\".format('Name', 'Last Name', 'Age', 'Country', 'City', 'Field', 'Institution', 'Hobby')) \n",
" for username in compas:\n",
" data_info = compas[username]\n",
" if data_info[\"country\"] == country:\n",
" print (\"{:<15} {:<20} {:<10} {:<10} {:<15} {:<25} {:<40} {:<10}\".format(data_info[\"name\"], data_info[\"lastname\"], data_info[\"age\"], data_info[\"country\"], data_info[\"city\"], data_info[\"field\"], data_info[\"institution\"], data_info[\"hobby\"]))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def get_age_avg():\n",
" if len(compas) == 0:\n",
" return 0\n",
" ages_sum = 0\n",
" for username in compas:\n",
" ages_sum += int(compas[username][\"age\"])\n",
"\n",
" return ages_sum / len(compas)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def get_institutions():\n",
" institutions = set()\n",
" for username in compas:\n",
" institutions.add(compas[username][\"institution\"])\n",
" \n",
" return institutions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:code id: tags:
```
python
#importar librerias a usar
import
csv
,
json
#En esta variable se guardan todas las filas del CSV
compas
=
{}
#previamente he cargado el archivo cega_tarea_2.csv en el directorio del pwd
FILE_NAME
=
'
cega_tarea_2.csv
'
#Indices para cada columna
USERNAME
=
0
NAME
=
1
LASTNAME
=
2
AGE
=
3
COUNTRY
=
4
CITY
=
5
FIELD
=
6
INSTITUTION
=
7
HOBBY
=
8
```
%% Cell type:code id: tags:
```
python
#Funcion para leer un archivo
def
read_file
():
#Abre el archivo .csv
with
open
(
FILE_NAME
)
as
csv_file
:
csv_reader
=
csv
.
reader
(
csv_file
,
delimiter
=
'
,
'
)
#Saltarse los encabezados de las columnas
header
=
True
#Para cada fila del archivo .csv
for
row
in
csv_reader
:
if
not
header
:
compas
[
row
[
USERNAME
]]
=
{
"
name
"
:
row
[
NAME
],
"
lastname
"
:
row
[
LASTNAME
],
"
age
"
:
row
[
AGE
],
"
country
"
:
row
[
COUNTRY
],
"
city
"
:
row
[
CITY
],
"
field
"
:
row
[
FIELD
],
"
institution
"
:
row
[
INSTITUTION
],
"
hobby
"
:
row
[
HOBBY
],
}
else
:
header
=
False
```
%% Cell type:code id: tags:
```
python
# Funcion principal
def
main
():
read_file
()
print
(
'
--------------------------
'
)
get_info_by_country
(
'
Colombia
'
)
print
(
'
--------------------------
'
)
print
(
f
'
Age avg:
{
get_age_avg
()
}
'
)
print
(
'
--------------------------
'
)
print
(
f
'
Institutions:
{
get_institutions
()
}
'
)
```
%% Cell type:code id: tags:
```
python
def
get_info_by_country
(
country
):
#print con formato
print
(
"
{:<15} {:<20} {:<10} {:<10} {:<15} {:<25} {:<40} {:<10}
"
.
format
(
'
Name
'
,
'
Last Name
'
,
'
Age
'
,
'
Country
'
,
'
City
'
,
'
Field
'
,
'
Institution
'
,
'
Hobby
'
))
for
username
in
compas
:
data_info
=
compas
[
username
]
if
data_info
[
"
country
"
]
==
country
:
print
(
"
{:<15} {:<20} {:<10} {:<10} {:<15} {:<25} {:<40} {:<10}
"
.
format
(
data_info
[
"
name
"
],
data_info
[
"
lastname
"
],
data_info
[
"
age
"
],
data_info
[
"
country
"
],
data_info
[
"
city
"
],
data_info
[
"
field
"
],
data_info
[
"
institution
"
],
data_info
[
"
hobby
"
]))
```
%% Cell type:code id: tags:
```
python
def
get_age_avg
():
if
len
(
compas
)
==
0
:
return
0
ages_sum
=
0
for
username
in
compas
:
ages_sum
+=
int
(
compas
[
username
][
"
age
"
])
return
ages_sum
/
len
(
compas
)
```
%% Cell type:code id: tags:
```
python
def
get_institutions
():
institutions
=
set
()
for
username
in
compas
:
institutions
.
add
(
compas
[
username
][
"
institution
"
])
return
institutions
```
%% Cell type:code id: tags:
```
python
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment