Está en la página 1de 4

Reemplazar esta línea con su número de documento de identificación (hacer doble clic aquí para editar) < 1

DATA STRUCTURES: Unit 2 project


Sonia Estefanía Mendía Martínez, José Manuel Solano Ochoa
Data Engineering, Universidad Politécnica de Yucatán
Km. 4.5. Carretera Mérida- Tetiz Trabaje Catastral 448. CP97357 Ucú, Yucatán.

2109104@upy.edu.mx,2009127@upy.edu.mx

sucursal = input (f'ingrese el nombre de la


sucursal {i+1}: ')
sucursales.append(sucursal)
INTRODUCTION M = int(input('Número de Años: '))

W e are going to look at two codes to obtain different types matriz = []


for i in range (M):
of sales data for a company, such as mean, total sales, maximum matriz.append ([])
and minimum sales in n years and n branch offices; the first one for j in range (N):
uses a basic algorithm run by a terminal in Spyder, and second valor= float(input (f'Ingrese ventas de la
method using Google Collab with NumPy and Pandas. sucursal {sucursales[j]} en el año {i+1} : '))
matriz [i].append(valor)

I. ALGORITHM RUN IN SPYDER #Proceso


print(' ')
print('DATOS OBTENIDOS')
Code: print(' ')
mayor = 0
''' for j in range(N):
Una empresa automotriz necesita un programa para suma = 0
manejar for i in range(M):
los montos de ventas de sus N sucursales, a lo largo suma = suma + matriz[i][j]
de los
últimos M años.
print (f'Número de ventas de la Sucursal
Los datos son dados de esta forma: M, N {sucursales[j]} es {suma}')
MONTO 1 1 MONTO 1 2 ….. MONTO 1 N if suma > mayor :
MONTO 2 1 MONTO 2 2 ….. MONTO 2 N mayor = suma
MONTO M 1 MONTO M 2 ….. MONTO M N sucMayor = sucursales[j]

Donde: print(' ')


M es una variable entera que representa el número de print(f'Sucursal que más vendió: {sucMayor}')
años print(' ')
entre 1 y 30 inclusive. print(' ')
N es una variable entera que representa el número de
sucursales de la empresa entre 1 y 35 inclusive. mayor = 0
MONTO i j Variable real (matriz de 2 dimensiones) for i in range(M):
representa lo que se vendió en el año I en la sucursal suma = 0
J for j in range(N):
La información que necesitan los directores de la suma = suma + matriz[i][j]
empresa promedio = suma/N
para tomar decisiones es la siguiente: print (f'Promedio de ventas del año {i+1} es
a. Sucursal que más ha vendido en los M años. {promedio}')
b. Promedio de ventas por año.
c. Año con mayor promedio de ventas.
''' if promedio > mayor :
mayor = promedio
#INPUT year = i + 1

N = int(input('Número de Sucursales a evaluar: ')) print(' ')


sucursales=[] print(f'El año con el mayor promedio de venta es:
for i in range (N): {year}')
Reemplazar esta línea con su número de documento de identificación (hacer doble clic aquí para editar) < 2

II. NUMPY AND PANDAS IN GOOGLE COLAB [1]


The whole code is moved by matrixes and ranges, so on the first
lines, we define the N branch offices (sucursales) the user will
introduce, being the range N any integer. Then, we do the same
with the M number of years, and finally, the introduction of the
names of each branch office.

Later on, we start with another defined matrix for the sales
values the user is going to introduce in a float type of value, and
we then use .append() to add the range of M (years) and the
range of N ( branch offices) to the matrix and to the printing
area as well.

In the second half of the code, we have our obtained data,


Fig 0.2.
starting with defining the maximum value which is equal to
zero, for the values introduced being all positive; Then, the sum In this notebook, we tried to solve the exact same problem but
adds the ranges [i] and [j] for the sales introduced on each with a more formal format as it is the Google Colab notebook,
branch office for the code to add them up and give the total sales as well as using the facilities of the libraries Python has in
value of each branch office. Subsequently, we made a NumPy to extract data and results of databases, Excel, Notes,
comparison to obtain with branch office sold the most in the M and tables.
years, so if the addition of a or b, or q is greater, the defined
function ‘sucMayor’ will give back the name of the greatest We first started by importing the necessary libraries for the
branch office. notebook to run accordingly. The difference here is that the way
to modify the data instead of introducing the values through a
Finally, we took out the mean of the sales for each year by console is by editing a table or an Excel file and then actualizing
adding again a range in the sum of the matrix [i][j] and dividing it, importing it, and extracting the values in the notebook. This
it by the N number of branch offices. The next and final step is is a very common way most of businesses and companies
the comparison, in which we analyze if the mean of M year is manage their data, and that´s why we wanted to use this type of
greater (j + 1). format, for a more professional approach.

This being said, there are two initial ways to import the
database: 1) we created a matrix with the corresponding values
of the office branches and the years of sales as it is in Fig. 0.2.,
or importing an Excel file, as it is shown in Fig. 0.3. and Fig
0.4., with the command pd.read_excel(‘File name’), after
defining the name of the function:

This:

Fig. 0.1

Fig 0.3.
Reemplazar esta línea con su número de documento de identificación (hacer doble clic aquí para editar) < 3

To this:

Fig.0.4.

Continuing with option no. 1, as a matrix is a bi-dimensional Fig 0.6.


table, the way to print the matrix in a more aesthetic format,
there´s a display() command that helps us display the The next step is calculating the total sales from both branch
DataFrame in the form of a table as it helps in proper and easy offices, which is done by defining a new function named
visualization of the data (Fig. 0.2.) ambas_suc and setting it equal to the main function which is
suc, defining the index from which we will be extracting our
Furthermore, we add an additional column to the table that will values which are [‘Total’], and finally adding the .sum().
add up all the sales values in all the years of each branch office, Following this, we extract the mean value of the last result by
this with adding to the defined function suc[‘Name of new just adding the .mean().
column’] and, then we add .sum() for the operation of addition,
and here in Pandas we need to call the axis of 0 when we are With the same logic, we then extracted the mean of each year
manipulating data from rows and axis of 1 when we are working of both branch offices, but now our indexes of suc will be the
with columns (axis = 1 in this case), and finally we define that columns of each year ([‘Año_1, Año_2, Año_3, …]). By
we are only manipulating numeric values for the addition so the obtaining the mean of each, we then can easily visualize the
algorithm doesn’t crack with text values (numeric_only = greatest mean and of which year is it from.
True).
Last but not least, for a better statistical understanding of the
Then, to know which branch office sold the most we used sales, we printed the database in a graphical form, by using the
.groupby(‘Name of both branch offices as lists’) function, function .plot(), which is used to draw points (markers) in a
which is used to split the data into groups based on some diagram. By default, the plot() function draws a line from point
criteria, and our criteria, in this case, is the greatest integer to point. The function takes parameters for specifying points in
number given in the Total, this complemented with the diagram. With this, we have a representation of how the
.sort_values(), which will sort the values the way we want by sales increased, decreased, or stayed the same during the years
using specific parameters, which they will be: ascending (see Fig. 0.7.).
function equal to False, to accommodate the results from
maximum to minimum and having as a result in the first row
the branch office with the major sale; if this database had more
branch office rows, this would be the easiest way to see which
ones had the top sales.

Fig 0.7.

Fig 0.5.
Reemplazar esta línea con su número de documento de identificación (hacer doble clic aquí para editar) < 4

REFERENCES
[1]
https://colab.research.google.com/drive/1PV2kkbK8Fo2USGGP4WeoZ8Zqx
g_aYiYd?usp=sharing

También podría gustarte