GMARTINEZMILLA commited on
Commit
7c03572
·
1 Parent(s): 8b97595

feat: updated app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -46
app.py CHANGED
@@ -234,79 +234,89 @@ elif page == "Customer Analysis":
234
  else:
235
  st.warning(f"No actual sales data found for customer {customer_code} in df_agg_2024.")
236
 
237
- # Show the radar chart
 
 
 
 
238
  all_manufacturers = customer_data.iloc[:, 1:].T # Exclude CLIENTE column
239
  all_manufacturers.index = all_manufacturers.index.astype(str)
240
 
241
- sales_data = euros_proveedor.iloc[:, 1:].T # Exclude CLIENTE column
 
 
242
  sales_data.index = sales_data.index.astype(str)
243
 
 
244
  sales_data_filtered = sales_data.drop(index='CLIENTE', errors='ignore')
 
 
245
  sales_data_filtered = sales_data_filtered.apply(pd.to_numeric, errors='coerce')
246
 
247
- # Ensure the values in all_manufacturers are numeric before sorting
248
- all_manufacturers_numeric = pd.to_numeric(all_manufacturers.iloc[:, 0], errors='coerce')
249
 
250
- # Sort the manufacturers based on numeric values
251
- top_units = all_manufacturers.assign(numeric_values=all_manufacturers_numeric).sort_values(by='numeric_values', ascending=False).head(10)
252
 
253
- # Drop the temporary numeric column after sorting
254
- top_units = top_units.drop(columns=['numeric_values'])
255
 
256
- # Ensure the values in sales_data_filtered are numeric before sorting
257
- sales_data_filtered_numeric = pd.to_numeric(sales_data_filtered.iloc[:, 0], errors='coerce')
258
 
259
- # Sort the sales data based on numeric values
260
- top_sales = sales_data_filtered.assign(numeric_values=sales_data_filtered_numeric).sort_values(by='numeric_values', ascending=False).head(10)
261
 
262
- # Drop the temporary numeric column after sorting
263
- top_sales = top_sales.drop(columns=['numeric_values'])
264
- combined_top = pd.concat([top_units, top_sales]).index.unique()[:20]
265
- combined_top = [m for m in combined_top if m in all_manufacturers.index and m in sales_data_filtered.index]
 
 
266
 
267
- combined_data = pd.DataFrame({
268
- 'units': all_manufacturers.loc[combined_top, all_manufacturers.columns[0]],
269
- 'sales': sales_data_filtered.loc[combined_top, sales_data_filtered.columns[0]]
270
- }).fillna(0)
271
 
272
- combined_data_sorted = combined_data.sort_values(by=['units', 'sales'], ascending=False)
273
- non_zero_manufacturers = combined_data_sorted[combined_data_sorted['units'] > 0]
274
 
275
- if len(non_zero_manufacturers) < 3:
276
- zero_manufacturers = combined_data_sorted[combined_data_sorted['units'] == 0].head(3 - len(non_zero_manufacturers))
277
- manufacturers_to_show = pd.concat([non_zero_manufacturers, zero_manufacturers])
278
- else:
279
- manufacturers_to_show = non_zero_manufacturers
 
280
 
281
- values = manufacturers_to_show['units'].tolist()
282
- amounts = manufacturers_to_show['sales'].tolist()
283
- manufacturers = [get_supplier_name(m) for m in manufacturers_to_show.index]
284
 
285
- st.write(f"### Results for top {len(manufacturers)} manufacturers:")
286
- for manufacturer, value, amount in zip(manufacturers, values, amounts):
287
- st.write(f"{manufacturer} = {value:.2f}% of units, €{amount:.2f} total sales")
 
 
 
 
 
 
288
 
289
- if manufacturers:
290
- fig = radar_chart(manufacturers, values, amounts, f'Radar Chart for Top {len(manufacturers)} Manufacturers of Customer {customer_code}')
291
- st.pyplot(fig)
292
  else:
293
- st.warning("No data available to create the radar chart.")
294
 
295
- # Show sales over the years graph
296
- sales_columns = ['VENTA_2021', 'VENTA_2022', 'VENTA_2023']
297
- if all(col in ventas_clientes.columns for col in sales_columns):
298
- years = ['2021', '2022', '2023']
299
- customer_sales = ventas_clientes[ventas_clientes['codigo_cliente'] == customer_code][sales_columns].values[0]
300
 
301
  fig_sales = px.line(x=years, y=customer_sales, markers=True, title=f'Sales Over the Years for Customer {customer_code}')
302
  fig_sales.update_layout(xaxis_title="Year", yaxis_title="Sales")
303
  st.plotly_chart(fig_sales)
304
  else:
305
- st.warning("Sales data for 2021-2023 not available.")
306
  else:
307
- st.warning(f"No prediction data found for customer {customer_code}.")
308
- else:
309
- st.warning(f"No data found for customer {customer_code}. Please check the code.")
310
  else:
311
  st.warning("Please select a customer.")
312
 
 
234
  else:
235
  st.warning(f"No actual sales data found for customer {customer_code} in df_agg_2024.")
236
 
237
+ st.write("### Debug Information for Radar Chart:")
238
+ st.write(f"Shape of customer_data: {customer_data.shape}")
239
+ st.write(f"Shape of euros_proveedor: {euros_proveedor.shape}")
240
+
241
+ # Get percentage of units sold for each manufacturer
242
  all_manufacturers = customer_data.iloc[:, 1:].T # Exclude CLIENTE column
243
  all_manufacturers.index = all_manufacturers.index.astype(str)
244
 
245
+ # Get total sales for each manufacturer
246
+ customer_euros = euros_proveedor[euros_proveedor["CLIENTE"] == str(customer_code)]
247
+ sales_data = customer_euros.iloc[:, 1:].T # Exclude CLIENTE column
248
  sales_data.index = sales_data.index.astype(str)
249
 
250
+ # Remove the 'CLIENTE' row from sales_data to avoid issues with mixed types
251
  sales_data_filtered = sales_data.drop(index='CLIENTE', errors='ignore')
252
+
253
+ # Ensure all values are numeric
254
  sales_data_filtered = sales_data_filtered.apply(pd.to_numeric, errors='coerce')
255
 
256
+ # Sort manufacturers by percentage of units and get top 10
257
+ top_units = all_manufacturers.sort_values(by=all_manufacturers.columns[0], ascending=False).head(10)
258
 
259
+ # Sort manufacturers by total sales and get top 10
260
+ top_sales = sales_data_filtered.sort_values(by=sales_data_filtered.columns[0], ascending=False).head(10)
261
 
262
+ # Combine top manufacturers from both lists and get up to 20 unique manufacturers
263
+ combined_top = pd.concat([top_units, top_sales]).index.unique()[:20]
264
 
265
+ # Filter out manufacturers that are not present in both datasets
266
+ combined_top = [m for m in combined_top if m in all_manufacturers.index and m in sales_data_filtered.index]
267
 
268
+ st.write(f"Number of combined top manufacturers: {len(combined_top)}")
 
269
 
270
+ if combined_top:
271
+ # Create a DataFrame with combined data for these top manufacturers
272
+ combined_data = pd.DataFrame({
273
+ 'units': all_manufacturers.loc[combined_top, all_manufacturers.columns[0]],
274
+ 'sales': sales_data_filtered.loc[combined_top, sales_data_filtered.columns[0]]
275
+ }).fillna(0)
276
 
277
+ # Sort by units, then by sales
278
+ combined_data_sorted = combined_data.sort_values(by=['units', 'sales'], ascending=False)
 
 
279
 
280
+ # Filter out manufacturers with 0 units
281
+ non_zero_manufacturers = combined_data_sorted[combined_data_sorted['units'] > 0]
282
 
283
+ # If we have less than 3 non-zero manufacturers, add some zero-value ones
284
+ if len(non_zero_manufacturers) < 3:
285
+ zero_manufacturers = combined_data_sorted[combined_data_sorted['units'] == 0].head(3 - len(non_zero_manufacturers))
286
+ manufacturers_to_show = pd.concat([non_zero_manufacturers, zero_manufacturers])
287
+ else:
288
+ manufacturers_to_show = non_zero_manufacturers
289
 
290
+ values = manufacturers_to_show['units'].tolist()
291
+ amounts = manufacturers_to_show['sales'].tolist()
292
+ manufacturers = [get_supplier_name(m) for m in manufacturers_to_show.index]
293
 
294
+ st.write(f"### Results for top {len(manufacturers)} manufacturers:")
295
+ for manufacturer, value, amount in zip(manufacturers, values, amounts):
296
+ st.write(f"{manufacturer} = {value:.2f}% of units, €{amount:.2f} total sales")
297
+
298
+ if manufacturers: # Only create the chart if we have data
299
+ fig = radar_chart(manufacturers, values, amounts, f'Radar Chart for Top {len(manufacturers)} Manufacturers of Customer {customer_code}')
300
+ st.pyplot(fig)
301
+ else:
302
+ st.warning("No data available to create the radar chart.")
303
 
 
 
 
304
  else:
305
+ st.warning("No combined top manufacturers found.")
306
 
307
+ # Customer sales 2021-2024 (if data exists)
308
+ sales_columns = ['VENTA_2021', 'VENTA_2022', 'VENTA_2023', 'VENTA_2024']
309
+ if all(col in df.columns for col in sales_columns):
310
+ years = ['2021', '2022', '2023', '2024']
311
+ customer_sales = customer_data[sales_columns].values[0]
312
 
313
  fig_sales = px.line(x=years, y=customer_sales, markers=True, title=f'Sales Over the Years for Customer {customer_code}')
314
  fig_sales.update_layout(xaxis_title="Year", yaxis_title="Sales")
315
  st.plotly_chart(fig_sales)
316
  else:
317
+ st.warning("Sales data for 2021-2024 not available.")
318
  else:
319
+ st.warning(f"No data found for customer {customer_code}. Please check the code.")
 
 
320
  else:
321
  st.warning("Please select a customer.")
322