Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import plotly.express as px | |
| import plotly.graph_objects as go | |
| # Configuración de la página principal | |
| st.set_page_config(page_title="Customer Insights App", page_icon=":bar_chart:") | |
| # Cargar el archivo CSV que ya está disponible en la web | |
| df = pd.read_csv("df_clean.csv") # Asegúrate de que la ruta del archivo es correcta | |
| # Ignorar las dos últimas columnas | |
| df = df.iloc[:, :-2] | |
| # Asegurarse de que el código del cliente sea una cadena (string) | |
| df['CLIENTE'] = df['CLIENTE'].astype(str) | |
| # Diseño de la página principal | |
| st.title("Welcome to Customer Insights App") | |
| st.markdown(""" | |
| This app helps businesses analyze customer behaviors and provide personalized recommendations based on purchase history. | |
| Use the tools below to dive deeper into your customer data. | |
| """) | |
| # Menú de navegación | |
| page = st.selectbox("Selecciona la herramienta que quieres utilizar", ["", "Customer Analysis", "Customer Recommendations"]) | |
| # Página Home | |
| if page == "": | |
| st.markdown("## Welcome to the Customer Insights App") | |
| st.write("Use the dropdown menu to navigate between the different sections.") | |
| # Página Customer Analysis | |
| elif page == "Customer Analysis": | |
| st.title("Customer Analysis") | |
| st.markdown(""" | |
| Use the tools below to explore your customer data. | |
| """) | |
| # Campo para filtrar clientes | |
| partial_code = st.text_input("Enter part of Customer Code (or leave empty to see all)") | |
| # Filtrar las opciones de clientes que coincidan con el código parcial | |
| if partial_code: | |
| filtered_customers = df[df['CLIENTE'].str.contains(partial_code)] | |
| else: | |
| filtered_customers = df | |
| # Crear una lista de clientes filtrados para el selectbox | |
| customer_list = filtered_customers['CLIENTE'].unique() | |
| # Selección de cliente con autocompletar filtrado | |
| customer_code = st.selectbox("Select Customer Code", customer_list) | |
| if customer_code: | |
| # Filtrar datos para el cliente seleccionado | |
| customer_data = df[df["CLIENTE"] == customer_code] | |
| if not customer_data.empty: | |
| st.write(f"### Analysis for Customer {customer_code}") | |
| # Obtener las 15 columnas con los valores más altos (ignorar la columna de cliente) | |
| top_15_manufacturers = customer_data.iloc[:, 1:].T.nlargest(15, customer_data.index[0]) | |
| # Generar el spider chart con los top 15 fabricantes | |
| fig_spider = go.Figure() | |
| fig_spider.add_trace(go.Scatterpolar( | |
| r=top_15_manufacturers[customer_data.index[0]].values, | |
| theta=top_15_manufacturers.index, | |
| fill='toself', | |
| name=f'Customer {customer_code}' | |
| )) | |
| # Ajustar el diseño del gráfico para que se vea más cerca y las variables queden bien distribuidas | |
| fig_spider.update_layout( | |
| polar=dict( | |
| radialaxis=dict( | |
| visible=True, | |
| range=[0, 1] # Ajusta este rango según la escala que deseas, aquí ajusto de 0 a 1 | |
| ), | |
| angularaxis=dict( | |
| tickmode='array', | |
| tickvals=[i * (360 / 15) for i in range(15)], # Asegura que haya 15 puntos distribuidos uniformemente | |
| ) | |
| ), | |
| showlegend=False, | |
| title=f'Spider Chart for Top 15 Manufacturers of Customer {customer_code}', | |
| height=600, # Puedes ajustar el tamaño si quieres más espacio | |
| width=600 | |
| ) | |
| st.plotly_chart(fig_spider) | |
| # Ventas del cliente 2021-2024 (si los datos existen) | |
| if 'VENTA_2021' in df.columns and 'VENTA_2022' in df.columns and 'VENTA_2023' in df.columns and 'VENTA_2024' in df.columns: | |
| years = ['2021', '2022', '2023', '2024'] | |
| sales_columns = ['VENTA_2021', 'VENTA_2022', 'VENTA_2023', 'VENTA_2024'] | |
| customer_sales = customer_data[sales_columns].values[0] | |
| fig_sales = px.line(x=years, y=customer_sales, markers=True, title=f'Sales Over the Years for Customer {customer_code}') | |
| fig_sales.update_layout(xaxis_title="Year", yaxis_title="Sales") | |
| st.plotly_chart(fig_sales) | |
| else: | |
| st.warning("Sales data for 2021-2024 not available.") | |
| else: | |
| st.warning(f"No data found for customer {customer_code}. Please check the code.") | |
| # Página Customer Recommendations | |
| elif page == "Customer Recommendations": | |
| st.title("Customer Recommendations") | |
| st.markdown(""" | |
| Get tailored recommendations for your customers based on their purchasing history. | |
| """) | |
| # Campo para filtrar clientes | |
| partial_code = st.text_input("Enter part of Customer Code for Recommendations (or leave empty to see all)") | |
| # Filtrar las opciones de clientes que coincidan con el código parcial | |
| if partial_code: | |
| filtered_customers = df[df['CLIENTE'].str.contains(partial_code)] | |
| else: | |
| filtered_customers = df | |
| # Crear una lista de clientes filtrados para el selectbox | |
| customer_list = filtered_customers['CLIENTE'].unique() | |
| # Selección de cliente con autocompletar filtrado | |
| customer_code = st.selectbox("Select Customer Code for Recommendations", customer_list) | |
| if customer_code: | |
| customer_data = df[df["CLIENTE"] == customer_code] | |
| if not customer_data.empty: | |
| # Mostrar historial de compras del cliente seleccionado | |
| st.write(f"### Purchase History for Customer {customer_code}") | |
| st.write(customer_data) | |
| # Generar recomendaciones (placeholder) | |
| st.write(f"### Recommended Products for Customer {customer_code}") | |
| # Aquí puedes reemplazar con la lógica del modelo de recomendación | |
| st.write("Product A, Product B, Product C") | |
| else: | |
| st.warning(f"No data found for customer {customer_code}. Please check the code.") | |