GMARTINEZMILLA commited on
Commit
6a893b4
1 Parent(s): 08f4cfc

feat: updated website

Browse files
Files changed (1) hide show
  1. app.py +25 -28
app.py CHANGED
@@ -366,75 +366,72 @@ elif page == "Customer Analysis":
366
  fecha_inicio = pd.to_datetime("2023-01-01")
367
  fecha_corte = pd.to_datetime("2024-09-01")
368
 
 
369
  historical_data['fecha_mes'] = pd.to_datetime(historical_data['fecha_mes'], errors='coerce')
370
-
371
- # Ensure the 'fecha_mes' column in results is also in datetime format if it's being used for comparisons
372
  results['fecha_mes'] = pd.to_datetime(results['fecha_mes'], errors='coerce')
373
 
374
- st.subheader("Datos hist贸ricos completos")
375
- st.write(historical_data.head())
376
-
377
- st.subheader("Resultados completos con predicciones")
378
- st.write(results.head())
379
-
380
-
381
- fecha_inicio = pd.to_datetime(fecha_inicio)
382
-
383
- # Filter historical data for the customer over the last 12 months
384
  datos_historicos = historical_data[
385
  (historical_data['cliente_id'] == customer_code_str) &
386
- (historical_data['fecha_mes'] >= fecha_inicio) &
387
  (historical_data['fecha_mes'] < fecha_corte)
388
  ].groupby('fecha_mes')['precio_total'].sum().reset_index()
389
- st.write(datos_historicos)
390
 
391
- # Rename 'precio_total' column to 'ventas_historicas'
392
  datos_historicos.rename(columns={'precio_total': 'ventas_historicas'}, inplace=True)
393
 
394
- # Filter prediction and real sales data and sum by month
395
  datos_cliente_total = results.groupby('fecha_mes').agg({
396
  'ventas_reales': 'sum',
397
  'ventas_predichas': 'sum'
398
  }).reset_index()
399
 
400
- # Combine historical data with prediction and real sales data
401
- datos_combinados = pd.concat([datos_historicos, datos_cliente_total], sort=False).sort_values('fecha_mes').reset_index(drop=True)
 
 
 
 
 
402
 
403
- # **Generate the graph**
 
 
404
 
 
405
  st.markdown("### Sales History, Predictions, and Real Sales")
406
 
407
- # Create the figure using Plotly
408
  fig = go.Figure()
409
 
410
- # Plot historical sales
411
  fig.add_trace(go.Scatter(
412
- x=datos_combinados['fecha_mes'], # Dates
413
  y=datos_combinados['ventas_historicas'],
414
  mode='lines+markers',
415
  name='Ventas Hist贸ricas',
416
  line=dict(color='blue')
417
  ))
418
 
419
- # Plot predicted sales
420
  fig.add_trace(go.Scatter(
421
- x=datos_combinados['fecha_mes'], # Dates
422
  y=datos_combinados['ventas_predichas'],
423
  mode='lines+markers',
424
  name='Ventas Predichas',
425
  line=dict(color='orange')
426
  ))
427
 
428
- # Plot real sales
429
  fig.add_trace(go.Scatter(
430
- x=datos_combinados['fecha_mes'], # Dates
431
  y=datos_combinados['ventas_reales'],
432
  mode='lines+markers',
433
  name='Ventas Reales',
434
  line=dict(color='green')
435
  ))
436
 
437
- # Customize the layout
438
  fig.update_layout(
439
  title=f"Ventas Hist贸ricas, Predichas y Reales para Cliente {customer_code}",
440
  xaxis_title="Fecha",
@@ -444,7 +441,7 @@ elif page == "Customer Analysis":
444
  hovermode="x unified"
445
  )
446
 
447
- # Display the Plotly figure
448
  st.plotly_chart(fig)
449
 
450
  # Split space into two columns
 
366
  fecha_inicio = pd.to_datetime("2023-01-01")
367
  fecha_corte = pd.to_datetime("2024-09-01")
368
 
369
+ # Convertir fecha_mes a datetime en ambos DataFrames
370
  historical_data['fecha_mes'] = pd.to_datetime(historical_data['fecha_mes'], errors='coerce')
 
 
371
  results['fecha_mes'] = pd.to_datetime(results['fecha_mes'], errors='coerce')
372
 
373
+ # Filtrar datos hist贸ricos por cliente y por el rango de fechas (2023)
 
 
 
 
 
 
 
 
 
374
  datos_historicos = historical_data[
375
  (historical_data['cliente_id'] == customer_code_str) &
376
+ (historical_data['fecha_mes'] >= fecha_inicio) &
377
  (historical_data['fecha_mes'] < fecha_corte)
378
  ].groupby('fecha_mes')['precio_total'].sum().reset_index()
 
379
 
380
+ # Renombrar columna precio_total a ventas_historicas
381
  datos_historicos.rename(columns={'precio_total': 'ventas_historicas'}, inplace=True)
382
 
383
+ # Filtrar predicciones y ventas reales (2024) y agrupar por mes
384
  datos_cliente_total = results.groupby('fecha_mes').agg({
385
  'ventas_reales': 'sum',
386
  'ventas_predichas': 'sum'
387
  }).reset_index()
388
 
389
+ # Combinar datos hist贸ricos con predicciones y ventas reales, usando un merge en lugar de concat
390
+ datos_combinados = pd.merge(datos_historicos, datos_cliente_total, on='fecha_mes', how='outer').sort_values('fecha_mes')
391
+
392
+ # Rellenar los NaN: 0 en ventas_historicas donde faltan predicciones, y viceversa
393
+ datos_combinados['ventas_historicas'].fillna(0, inplace=True)
394
+ datos_combinados['ventas_predichas'].fillna(0, inplace=True)
395
+ datos_combinados['ventas_reales'].fillna(0, inplace=True)
396
 
397
+ # Mostrar los datos combinados
398
+ st.subheader("Datos combinados con valores faltantes llenados")
399
+ st.write(datos_combinados)
400
 
401
+ # **Generar la gr谩fica**
402
  st.markdown("### Sales History, Predictions, and Real Sales")
403
 
404
+ # Crear la gr谩fica con Plotly
405
  fig = go.Figure()
406
 
407
+ # Graficar ventas hist贸ricas
408
  fig.add_trace(go.Scatter(
409
+ x=datos_combinados['fecha_mes'],
410
  y=datos_combinados['ventas_historicas'],
411
  mode='lines+markers',
412
  name='Ventas Hist贸ricas',
413
  line=dict(color='blue')
414
  ))
415
 
416
+ # Graficar ventas predichas
417
  fig.add_trace(go.Scatter(
418
+ x=datos_combinados['fecha_mes'],
419
  y=datos_combinados['ventas_predichas'],
420
  mode='lines+markers',
421
  name='Ventas Predichas',
422
  line=dict(color='orange')
423
  ))
424
 
425
+ # Graficar ventas reales
426
  fig.add_trace(go.Scatter(
427
+ x=datos_combinados['fecha_mes'],
428
  y=datos_combinados['ventas_reales'],
429
  mode='lines+markers',
430
  name='Ventas Reales',
431
  line=dict(color='green')
432
  ))
433
 
434
+ # Personalizar layout
435
  fig.update_layout(
436
  title=f"Ventas Hist贸ricas, Predichas y Reales para Cliente {customer_code}",
437
  xaxis_title="Fecha",
 
441
  hovermode="x unified"
442
  )
443
 
444
+ # Mostrar la gr谩fica en Streamlit
445
  st.plotly_chart(fig)
446
 
447
  # Split space into two columns