# Exploración del df de artículos
print('Muestra de datos')
print(df_articles.head()) # head() 5 Filas por defecto
print('\nFormato del dataframe')
print(df_articles.shape)
print('\nBusqueda de valores nuls por columna')
print(df_articles.isnull().sum())
print('\nFormato de los datos por columna')
print(df_articles.dtypes)
Muestra de datos
article_id article_name unit_price
0 20015 Smartphone 525.00
1 20016 Full Pc 2127.81
2 20017 Monitor 230.00
3 20018 Tablet 130.00
4 20019 Desk 130.10
Formato del dataframe
(31, 3)
Busqueda de valores nuls por columna
article_id 0
article_name 0
unit_price 0
dtype: int64
Formato de los datos por columna
article_id int64
article_name object
unit_price object
dtype: object
# Exploración del df de vendedores
print('Muestra de datos')
print(df_sellers.head()) # head() 5 Filas por defecto
print('\nFormato del dataframe')
print(df_sellers.shape)
print('\nBusqueda de valores nuls por columna')
print(df_sellers.isnull().sum())
print('\nFormato de los datos por columna')
print(df_sellers.dtypes)
Muestra de datos
seller_name
seller_id
1 Aveline Swanwick
2 Jase Doy
3 Oliviero Charkham
4 Cornie Wynrehame
5 Ewell Peres
Formato del dataframe
(15, 1)
Busqueda de valores nuls por columna
seller_name 0
dtype: int64
Formato de los datos por columna
seller_name object
dtype: object
# Exploración del df de órdenes
print('Muestra de datos')
print(df_orders.head()) # head() 5 Filas por defecto
print('\nFormato del dataframe')
print(df_orders.shape)
print('\nBusqueda de valores nuls por columna')
print(df_orders.isnull().sum())
print('\nFormato de los datos por columna')
print(df_orders.dtypes)
Muestra de datos
order_id week article_id quantity seller_id country_name
0 15024 1 20039 10 10 Peru
1 15025 1 20029 15 5 Peru
2 15026 1 20024 5 14 Bolivia
3 15027 1 20018 9 14 Brazil
4 15028 1 20035 6 15 Mexico
Formato del dataframe
(1000, 6)
Busqueda de valores nuls por columna
order_id 0
week 0
article_id 0
quantity 0
seller_id 0
country_name 0
dtype: int64
Formato de los datos por columna
order_id int64
week int64
article_id int64
quantity int64
seller_id int64
country_name object
dtype: object
my_df = df_orders.copy() # shallow copy o copia superficial (otro Objeto alojado en la memoria)
# Cambio el indice del df de artículos
df_articles.set_index('article_id', inplace=True)
my_df = my_df.assign(article_name = my_df['article_id'])
my_df = my_df.assign(total_amount = my_df['article_id'])
my_df = my_df.assign(seller_name = my_df['seller_id'])
# df_articles
# print()
# my_df
for i in range(max(my_df.count())):
# SINTAXIS: df_articles.loc[indice][columna]
# [indice] va a ser el dato que obtengo de my_df.loc[i]['article_id']
# [indice] -> [my_df.loc[i]['article_id']]
# o sea, tomo registro a registro el article_id y lo uso para extraer el nombre del artículo (article_name)
# print(df_articles.loc[my_df.loc[i]['article_id']]['article_name'])
article = df_articles.loc[my_df.loc[i]['article_id']]['article_name']
# print(article)
# Asignar a cada valor id de la columna 'article_name' (my_df) el nombre del artículo
my_df.loc[i, 'article_name'] = article
# hacemos lo mismo con total amount
my_df.loc[i, 'total_amount'] = my_df.loc[i, 'quantity']*df_articles.loc[my_df.loc[i]['article_id']]['unit_price']
# columna de seller_name
my_df.loc[i, 'seller_name'] = df_sellers.loc[my_df.loc[i]['seller_id']]['seller_name']
# elimino las columnas que no necesito del df
my_df.drop(['order_id', 'article_id', 'seller_id'], axis='columns', inplace=True)
print(my_df)
week quantity country_name article_name total_amount seller_name
0 1 10 Peru Water Cooling 675.0 Cirilo Grandham
1 1 15 Peru Mouse 454.5 Ewell Peres
2 1 5 Bolivia Netbook 725.0 Janel O'Curran
3 1 9 Brazil Tablet 1170.0 Janel O'Curran
4 1 6 Mexico Case 227.4 Daisie Slograve
.. ... ... ... ... ... ...
995 4 1 Brazil Modem 67.5 Kati Innot
996 4 15 Brazil Heatsink 150.0 Daisie Slograve
997 4 2 Colombia Heatsink 20.0 Vasily Danilyuk
998 4 14 Brazil Tablet 1820.0 Vasily Danilyuk
999 4 12 Brazil SDD 264.0 Onida Cosely
[1000 rows x 6 columns]
# RESOLUCIÓN ANALÍTICA
print("Los artículos y sus cantidades vendidas")
print(my_df['article_name'].value_counts()) # cuenta valores únicos
Los artículos y sus cantidades vendidas
HDD 47
SDD 45
Netbook 45
Tablet 40
Usb Cable 39
Sata Cable 38
Pci Express Port 37
Range Extender 36
Smartphone 35
Full Pc 34
Headphones 34
Heatsink 34
Power Supply 34
Mouse 34
Motherboard 33
Water Cooling 32
Ram Memory 31
Video Card 31
Notebook 30
CPU 29
Modem 29
Desk 28
Webcam 28
Mesh Wi-Fi X 2 28
Monitor 26
Case 26
Fan Cooler 25
Scanner 24
Chair 24
Wi-Fi Card 22
Keyboard 22
Name: article_name, dtype: int64
# RESOLUCIÓN GRÁFICA
df0 = (my_df.groupby(by='article_name').sum()).sort_values('total_amount', ascending=False)
plt.bar(df0.index,df0 ['total_amount'], color='lightsalmon')
plt.title("UNIDADES VENDIDAS POR ARTÍCULO")
plt.xlabel("UNIDADES VENDIDAS")
plt.ylabel("ARTÍCULO")
plt.xticks(rotation=90)
plt.show()
# RESOLUCIÓN ANALÍTICA
df2 = (my_df.groupby(by='article_name').sum()).sort_values('total_amount', ascending=False).head(5)
pd.options.display.float_format='$ {:,.2f}'.format
print(df2['total_amount'])
article_name
Full Pc $ 538,335.93
Notebook $ 251,000.00
Smartphone $ 152,250.00
Chair $ 69,477.48
Tablet $ 48,620.00
Name: total_amount, dtype: float64
# RESOLUCIÓN GRÁFICA
c=['crimson', 'gold', 'chocolate', 'plum', 'mediumseagreen']
plt.pie(x=df2['total_amount'], autopct='%1.1f%%', colors=c, labels=df2.index)
plt.show()
# index es el 'article_name' del df2
# RESOLUCIÓN ANALÍTICA
df4 = (my_df.groupby(by='seller_name').sum()).sort_values('total_amount', ascending=False)
pd.options.display.float_format='$ {:,.2f}'.format
# print(df4[['quantity'] + ['total_amount']])
print(df4[['quantity'] + ['total_amount']].head(5))
quantity total_amount
seller_name
Janel O'Curran 703 $ 192,832.47
Brockie Patience 441 $ 142,709.88
Oliviero Charkham 555 $ 141,329.76
Vasily Danilyuk 521 $ 129,157.55
Daisie Slograve 554 $ 120,520.11
# RESOLUCIÓN GRÁFICA
plt.bar(df4.index,df4 ['total_amount'], color='darkcyan')
plt.title("VENTAS TOTALES POR VENDEDOR")
plt.xlabel("Nombre del vendedor")
plt.ylabel("Ventas totales")
plt.xticks(rotation=90)
plt.show()
# RESOLUCIÓN ANALÍTICA
df5 = (my_df.groupby(by='week').sum()).sort_values('total_amount', ascending=False)
pd.options.display.float_format='$ {:,.1f}'.format
print(df5)
quantity total_amount
week
1 2449 $ 507,458.8
2 2444 $ 415,364.4
3 2114 $ 329,140.0
4 1058 $ 223,844.6
# RESOLUCIÓN GRÁFICA
plt.bar(df5.index, df5['total_amount'], color='mediumpurple')
plt.title("EVOLUCIÓN DE VENTAS POR SEMANA")
plt.xlabel("Semanas")
plt.ylabel("Ventas totales")
plt.xticks(np.arange(0,5,1))
plt.show()
print("Total de ventas por países y cantidad de productos vendidos")
df6 = (my_df.groupby(by='country_name').sum()).sort_values('total_amount', ascending=False)
print(df6[['total_amount'] + ['quantity']].head(20))
Total de ventas por países y cantidad de productos vendidos
total_amount quantity
country_name
Brazil $ 441,271.9 2515
Argentina $ 205,832.8 947
Colombia $ 177,514.3 881
Peru $ 161,421.1 1027
Mexico $ 138,620.0 846
Venezuela $ 77,684.5 320
El Salvador $ 57,391.3 111
Guatemala $ 52,579.2 202
Honduras $ 36,763.6 303
Costa Rica $ 34,606.5 145
Chile $ 24,661.0 231
Bolivia $ 22,682.8 181
Uruguay $ 17,843.1 92
Ecuador $ 17,475.3 129
Paraguay $ 8,195.1 123
Puerto Rico $ 1,265.4 12
BRA_df = my_df[(my_df['country_name'] == 'Brazil')]
BRA_df
weekint64
1 - 4
quantityint64
1 - 15
459
2
14
460
2
8
466
2
4
467
2
5
470
2
14
474
2
7
478
2
2
480
2
2
481
2
1
484
2
15
PRI_df = my_df[(my_df['country_name'] == 'Puerto Rico')]
PRI_df
weekint64
quantityint64
116
1
9
465
2
3
print("RESOLUCIÓN GRÁFICA")
sns.barplot(df6.index, df6['total_amount'],palette='copper')
plt.title("VENTAS TOTALES POR PAÍS")
plt.xlabel("País")
plt.ylabel("Ventas totales")
plt.xticks(rotation=90)
plt.show()
RESOLUCIÓN GRÁFICA
/shared-libs/python3.9/py/lib/python3.9/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
warnings.warn(
print("Artículo con más ingresos en ventas en Brasil:")
df7 = (BRA_df.groupby(by='article_name').sum()).sort_values('total_amount', ascending=False)
print(df7[['total_amount'] + ['quantity']].head(1))
print()
print("Artículo más elegido (en unidades):")
df7b = (BRA_df.groupby(by='article_name').sum()).sort_values('quantity', ascending=False)
print(df7b[['quantity'] + ['total_amount']].head(1))
print()
print("Artículo con menos ingresos en ventas en Brasil:")
df7c = (BRA_df.groupby(by='article_name').sum()).sort_values('total_amount', ascending=True)
print(df7c[['total_amount'] + ['quantity']].head(1))
print()
print("Artículo menos elegido (en unidades):")
df7d = (BRA_df.groupby(by='article_name').sum()).sort_values('quantity', ascending=True)
print(df7d[['quantity'] + ['total_amount']].head(1))
Artículo con más ingresos en ventas en Brasil:
total_amount quantity
article_name
Full Pc $ 134,052.0 63
Artículo más elegido (en unidades):
quantity total_amount
article_name
Tablet 156 $ 20,280.0
Artículo con menos ingresos en ventas en Brasil:
total_amount quantity
article_name
Sata Cable $ 122.0 57
Artículo menos elegido (en unidades):
quantity total_amount
article_name
Monitor 19 $ 4,370.0
print("RESOLUCIÓN GRÁFICA")
sns.barplot(df7.index, df7['total_amount'],palette='Spectral_r')
plt.xticks(rotation=90)
plt.title("VENTAS TOTALES POR ARTÍCULO EN BRASIL")
plt.xlabel("Artículo")
plt.ylabel("Ventas totales")
plt.show()
sns.barplot(df7b.index, df7b['quantity'],palette='afmhot')
plt.xticks(rotation=90)
plt.title("CANTIDADES VENDIDAS POR ARTÍCULO EN BRASIL")
plt.xlabel("Artículo")
plt.ylabel("Cantidades totales")
plt.show()
RESOLUCIÓN GRÁFICA
/shared-libs/python3.9/py/lib/python3.9/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
warnings.warn(
/shared-libs/python3.9/py/lib/python3.9/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
warnings.warn(
ARG_df = my_df[(my_df['country_name'] == 'Argentina')]
ARG_df
weekint64
1 - 4
quantityint64
1 - 15
886
4
7
902
4
4
908
4
15
915
4
5
918
4
7
928
4
11
930
4
4
941
4
9
943
4
1
948
4
7
print("Artículo con más ingresos en ventas en Argentina:")
df8 = (ARG_df.groupby(by='article_name').sum()).sort_values('total_amount', ascending=False)
print(df8[['total_amount'] + ['quantity']].head(1))
print()
print("Artículo más elegido (en unidades):")
df8b = (ARG_df.groupby(by='article_name').sum()).sort_values('quantity', ascending=False)
print(df8b[['quantity'] + ['total_amount']].head(1))
print()
print("Artículo con menos ingresos en ventas en Argentina:")
df8c = (ARG_df.groupby(by='article_name').sum()).sort_values('total_amount', ascending=True)
print(df8c[['total_amount'] + ['quantity']].head(1))
print()
print("Artículo menos elegido (en unidades):")
df8d = (ARG_df.groupby(by='article_name').sum()).sort_values('quantity', ascending=True)
print(df8d[['quantity'] + ['total_amount']].head(1))
Artículo con más ingresos en ventas en Argentina:
total_amount quantity
article_name
Full Pc $ 95,751.4 45
Artículo más elegido (en unidades):
quantity total_amount
article_name
CPU 104 $ 14,520.5
Artículo con menos ingresos en ventas en Argentina:
total_amount quantity
article_name
Pci Express Port $ 70.8 7
Artículo menos elegido (en unidades):
quantity total_amount
article_name
Chair 6 $ 2,013.8
print("RESOLUCIÓN GRÁFICA")
sns.barplot(df8.index, df8['total_amount'], palette='vlag')
plt.xticks(rotation=90)
plt.title("VENTAS TOTALES POR ARTÍCULO EN ARGENTINA")
plt.xlabel("Artículo")
plt.ylabel("Ventas totales")
plt.show()
sns.barplot(df8b.index, df8b['quantity'],palette='Wistia')
plt.xticks(rotation=90)
plt.title("CANTIDADES VENDIDAS POR ARTÍCULO EN ARGENTINA")
plt.xlabel("Artículo")
plt.ylabel("Cantidades totales")
plt.show()
RESOLUCIÓN GRÁFICA
/shared-libs/python3.9/py/lib/python3.9/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
warnings.warn(
/shared-libs/python3.9/py/lib/python3.9/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
warnings.warn(
print("Evolución de las ventas del artículo -Full Pc- por semana en Brasil")
df9a=BRA_df[(BRA_df['article_name'] == 'Full Pc')]
df9b=(df9a.groupby(by='week').sum()).sort_values('total_amount', ascending=False)
print(df9b)
print()
print("Evolución de las ventas del artículo -Full Pc- por semana en Argentina")
df9c=ARG_df[(ARG_df['article_name'] == 'Full Pc')]
df9d=(df9c.groupby(by='week').sum()).sort_values('week', ascending=True)
print(df9d)
print()
print("Evolución de las ventas del artículo -Sata Cable- por semana en Brasil")
df9e=BRA_df[(BRA_df['article_name'] == 'Sata Cable')]
df9f=(df9e.groupby(by='week').sum()).sort_values('total_amount', ascending=False)
print(df9f)
print()
print("Evolución de las ventas del artículo -Pci Express Port- por semana en Argentina")
df9g=ARG_df[(ARG_df['article_name'] == 'Pci Express Port')]
df9h=(df9g.groupby(by='week').sum()).sort_values('total_amount', ascending=False)
print(df9h)
Evolución de las ventas del artículo -Full Pc- por semana en Brasil
quantity total_amount
week
1 34 $ 72,345.5
2 22 $ 46,811.8
3 7 $ 14,894.7
Evolución de las ventas del artículo -Full Pc- por semana en Argentina
quantity total_amount
week
1 15 $ 31,917.1
2 26 $ 55,323.1
4 4 $ 8,511.2
Evolución de las ventas del artículo -Sata Cable- por semana en Brasil
quantity total_amount
week
1 26 $ 55.6
2 16 $ 34.2
3 15 $ 32.1
Evolución de las ventas del artículo -Pci Express Port- por semana en Argentina
quantity total_amount
week
1 7 $ 70.8
print("RESOLUCIÓN GRÁFICA")
sns.catplot(x="article_name", y="total_amount",hue="week", kind="box", palette='nipy_spectral',data=df9a)
sns.catplot(x="article_name", y="total_amount",hue="week", kind="box", palette='gnuplot2',data=df9e)
RESOLUCIÓN GRÁFICA
sns.catplot(x="article_name", y="total_amount",hue="week", kind="box", palette='CMRmap',data=df9c)
sns.catplot(x="article_name", y="total_amount",hue="week", kind="bar", palette='bone',data=df9g)