Скрипт для сегментации клиентской базы на множество категорий в зависимости от сумм, периодичности покупок и множества личных показателей (более 100 параметров)
Python tool for customer lifecycle segmentation and churn analysis using SQL sales history.
This project analyzes customer activity based on historical purchase data and automatically segments customers by lifecycle stage.
The script processes sales history, identifies customer behavior patterns and creates segmentation reports for CRM and marketing analytics.
Important: This project requires access to a configured SQL database with customer transaction history. It cannot be executed without replacing database integration with your own data source.
The main objective is to understand customer lifecycle and improve retention strategy.
The analysis helps answer:
- How many customers are active?
- Which customers are new?
- Which customers returned after inactivity?
- How many customers are at risk of churn?
- Which customers are already lost?
The model classifies customers into:
- New customers
- Regular customers
- Returned customers
- Sleeping customers
- Lost customers
- SQL database connection
- Sales history extraction
- Customer phone normalization
- Retail customer segmentation
- Service customer segmentation
- Combined company-wide segmentation
- Customer lifecycle classification
- Churn detection
- Excel report generation
- SQL database update
- Telegram execution notifications
- Parallel execution with threading
- Python
- pandas
- SQLAlchemy
- PyMySQL
- mysql-connector-python
- numpy
- tqdm
- pyTelegramBotAPI
- openpyxl
- python-dotenv
-
Loads customer transaction history from SQL:
- retail sales
- service sales
-
Cleans and standardizes customer phone numbers
-
Defines activity periods:
- last 6 months
- previous 6 months
- older history
-
Classifies customers by behavior
-
Generates customer segments:
- new
- regular
- returned
- sleeping
- lost
-
Creates Excel reports
-
Updates SQL segmentation tables
SQL database containing:
- Customer identifiers
- Purchase dates
- Sales channels
Excel reports:
- Retail customer segments
- Service customer segments
- Full company segmentation
- Segment summary statistics
SQL tables:
- segments_parts
- segments_sto
- segments_company
This project can be used for:
- CRM analytics
- customer retention analysis
- churn prevention
- loyalty strategy
- marketing segmentation