-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_code.py
348 lines (297 loc) · 11.7 KB
/
main_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
#FUNCION MENU PARA MOSTRAR LAS OPCIONES EN LA CONSOLA
def menu():
print("********************************************BIENVENIDO********************************************")
print("""Elige una opcion que quieras conocer por medio de una grafica:""")
print("1.Graficar la cantidad de videojuegos publicados con respecto al Año de Lanzamiento ")
print("2.Graficar las Ventas Globales con respecto al Género del videojuego")
print("3.Graficar las Ventas Globales con respecto a la Compañía de videojuegos ")
print("4.Graficar el Género del videojuego con respecto a los Años de lanzamiento")
print("5.Grafica la cantidad de videojuegos que hay por Género ")
print("6.Graficar las Ventas de NorteAmérica con respecto al año")
print("7.Graficar las Ventas Japón con respecto al año")
print("8.Graficar las Ventas Otros con respecto al año")
print("9.Determinar el top 3 de videojuegos más populares de cada género")
print("10.Determinar qué compañía genera mayores ventas.")
print("11.Determinar el porcentaje que aporta cada área geográfica a las Ventas Globales")
print("Otro numero para salir")
num = int(input("Ingresa el numero de la opcion que deseas: "))
return num
#FUNCION PARA SEPARAR EN 1 SOLO ELEMENTO LOS DATOS REPETIDOS
def separador(lista_init):
lista_fin = []
for i in lista_init:
try:
if (str(int(i))) not in lista_fin:
lista_fin.append(str(int(i)))
except:
if (str(i)) not in lista_fin:
lista_fin.append(str(i))
return lista_fin
#FUNCION PARA ORDENAR 2 LISTAS DE MAYOR A MENOR SEGUN LA SEGUNDA LISTA
def sorter_double(lista1, lista2):
lista_3 = [x for _,x in sorted(zip(lista2,lista1),reverse=True)]
lista2.sort(reverse=True)
return lista_3, lista2
#Graficar la cantidad de videojuegos publicados con respecto al Año de Lanzamiento
def publicaciones(data):
list_years = data["Year"].tolist()
list_only_one_year = separador(list_years)
list_only_one_year.sort()
list_only_one_year.remove("nan")
list_count = [0 for i in range(len(list_only_one_year))]
for i in list_years:
try:
list_count[list_only_one_year.index(str(int(i)))] += 1
except:
pass
plt.bar(list_only_one_year, list_count)
plt.title("Cantidad de videojuegos publicados segun el año")
plt.xlabel("Año de Lanzamiento")
plt.ylabel("Cantidad de videojuegos publicados")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#Graficar las Ventas Globales con respecto al Género del videojuegos
def global_sales_genre(data):
genero = data["Genre"].tolist()
global_sales = data["Global_Sales"].tolist()
list_genero = separador(genero)
list_count = [0 for i in range(len(list_genero))]
for i in range(len(genero)):
list_count[list_genero.index(str(genero[i]))] += round(global_sales[i],2)
list_count = [round(i,2) for i in list_count]
print(list_genero)
print(list_count)
plt.bar(list_genero, list_count)
plt.title("Ventas globales segun el género del videojuego")
plt.xlabel("Género del videojuego")
plt.ylabel("Ventas globales en Millones")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#Graficar las Ventas Globales con respecto a la Compañía de videojuego
def global_sales_publisher(data):
publicadores = data["Publisher"].tolist()
global_sales = data["Global_Sales"].tolist()
list_publicadores = separador(publicadores)
list_count = [0 for i in range(len(list_publicadores))]
for i in range(len(publicadores)):
list_count[list_publicadores.index(str(publicadores[i]))] += round(global_sales[i],2)
list_count = [round(i,2) for i in list_count]
list_publicadores, list_count = sorter_double(list_publicadores, list_count)
plt.bar(list_publicadores, list_count)
plt.title("Ventas globales de cada compañia")
plt.xlabel("Compañias")
plt.ylabel("Ventas globales en Millones")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#
#Graficar el Género del videojuego con respecto a los Años de lanzamiento
def genero_year(data):
genero = data["Genre"].tolist()
years = data["Year"].tolist()
list_years = separador(years)
list_years.remove("nan")
list_years.sort()
list_count=[]
for j in list_years:
list_micro_count = []
for i in range(len(genero)):
try:
comp = str(int(years[i]))
except:
comp = str(years[i])
if comp == j:
if genero[i] not in list_micro_count:
list_micro_count.append(genero[i])
list_count.append(len(list_micro_count))
plt.bar(list_years, list_count)
plt.title("Cantidad de generos de videojuegos publicados segun el año")
plt.xlabel("Año de Lanzamiento")
plt.ylabel("Cantidad de generos de videojuegos publicados")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#Graficar la cantidad de videojuegos que hay por Género
def videojuegos_genero(data):
genero = data["Genre"].tolist()
list_genero = separador(genero)
list_count = [0 for i in range(len(list_genero))]
for i in range(len(genero)):
list_count[list_genero.index(str(genero[i]))] += 1
plt.bar(list_genero, list_count)
plt.title("Cantidad de videojuegos por género")
plt.xlabel("Género del videojuego")
plt.ylabel("Cantidad de videojuegos")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#Graficar las Ventas de NorteAmérica con respecto al año
def north_america_sales_year(data):
years = data["Year"].tolist()
north_america_sales = data["NA_Sales"].tolist()
list_years = separador(years)
list_years.remove("nan")
list_years.sort()
list_count = [0 for i in range(len(list_years))]
for i in range(len(years)):
try:
list_count[list_years.index(str(int(years[i])))] += round(north_america_sales[i],2)
except:
pass
list_count = [round(i,2) for i in list_count]
plt.bar(list_years, list_count)
plt.title("Ventas de NorteAmérica segun el año")
plt.xlabel("Año de Lanzamiento")
plt.ylabel("Ventas de NorteAmérica en Millones")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#Graficar las Ventas Japón con respecto al año
def japan_sales_year(data):
years = data["Year"].tolist()
japan_sales = data["JP_Sales"].tolist()
list_years = separador(years)
list_years.remove("nan")
list_years.sort()
list_count = [0 for i in range(len(list_years))]
for i in range(len(years)):
try:
list_count[list_years.index(str(int(years[i])))] += round(japan_sales[i],2)
except:
pass
list_count = [round(i,2) for i in list_count]
plt.bar(list_years, list_count)
plt.title("Ventas de Japón segun el año")
plt.xlabel("Año de Lanzamiento")
plt.ylabel("Ventas de Japón en Millones")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#Graficar las Ventas Otros con respecto al año
def other_sales_year(data):
years = data["Year"].tolist()
other_sales = data["Other_Sales"].tolist()
list_years = separador(years)
list_years.remove("nan")
list_years.sort()
list_count = [0 for i in range(len(list_years))]
for i in range(len(years)):
try:
list_count[list_years.index(str(int(years[i])))] += round(other_sales[i],2)
except:
pass
list_count = [round(i,2) for i in list_count]
plt.bar(list_years, list_count)
plt.title("Ventas de Otros segun el año")
plt.xlabel("Año de Lanzamiento")
plt.ylabel("Ventas de Otros en Millones")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#Determinar el top 3 de videojuegos más populares de cada género.
def top_3_genero(data):
genero = data["Genre"].tolist()
videojuego = data["Name"].tolist()
global_sales = data["Global_Sales"].tolist()
gen_new = separador(genero)
list_top3 = [[] for i in gen_new]
for i in range(len(global_sales)):
if len(list_top3[gen_new.index(genero[i])]) != 3:
list_top3[gen_new.index(genero[i])].append([videojuego[i], global_sales[i]])
list_x = []
list_y = []
option = 0
for i in list_top3[option]:
list_x.append(i[0])
list_y.append(i[1])
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
def func(pct, allvals):
absolute = int(np.round(pct/100.*np.sum(allvals)))
return f"{pct:.1f}%\n({absolute:d} millones)"
wedges, texts, autotexts = ax.pie(list_y, autopct=lambda pct: func(pct, list_y),
textprops=dict(color="w"))
ax.legend(wedges, list_x,
title="Videojuegos",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=8, weight="bold")
ax.set_title(f"Top 3 de videojuegos más populares de {gen_new[option]}")
plt.show()
#Determinar qué compañía genera mayores ventas.
def top_companies(data):
publicadores = data["Publisher"].tolist()
global_sales = data["Global_Sales"].tolist()
list_publicadores = separador(publicadores)
list_count = [0 for i in range(len(list_publicadores))]
for i in range(len(publicadores)):
list_count[list_publicadores.index(str(publicadores[i]))] += round(global_sales[i],2)
list_count = [round(i,2) for i in list_count]
list_publicadores, list_count = sorter_double(list_publicadores, list_count)
print(len(list_publicadores))
list_publicadores = list_publicadores[:10]
list_count = list_count[:10]
plt.bar(list_publicadores, list_count)
plt.title("Top 10 compañias con mas ventas")
plt.xlabel("Compañias")
plt.ylabel("Ventas globales en Millones")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#.Determinar el porcentaje que aporta cada área geográfica a las Ventas Globales
def graphic_area(data):
na_sales = data["NA_Sales"].tolist()
eu_sales = data["EU_Sales"].tolist()
jp_sales = data["JP_Sales"].tolist()
other_sales = data["Other_Sales"].tolist()
list_types = ["NA_Sales", "EU_Sales", "JP_Sales", "Other_Sales"]
list_sales = [sum(na_sales), sum(eu_sales), sum(jp_sales), sum(other_sales)]
list_types, list_sales = sorter_double(list_types, list_sales)
plt.bar(list_types, list_sales)
plt.title("Ventas de cada zona")
plt.xlabel("Compañias")
plt.ylabel("Ventas en Millones")
plt.xticks(rotation=80)
plt.tight_layout()
plt.show()
#Main Function
def main():
respuesta = "si"
data = pd.read_csv("vgsales.csv")
while respuesta.lower() in ("si", "si"):
os.system("clear")
num = menu()
if num == 1:
publicaciones(data)
elif num == 2:
global_sales_genre(data)
elif num == 3:
global_sales_publisher(data)
elif num == 4:
genero_year(data)
elif num == 5:
videojuegos_genero(data)
elif num == 6:
north_america_sales_year(data)
elif num == 7:
japan_sales_year(data)
elif num == 8:
other_sales_year(data)
elif num == 9:
top_3_genero(data)
elif num == 10:
top_companies(data)
elif num == 11:
graphic_area(data)
else:
print("Gracias por usar el programa")
break
print("Desea volver al menu principal?")
respuesta = input( "Si/No: ").lower()
if __name__ == "__main__":
main()