Ce notebook vous guide pour créer un jeu de données complet et réaliste pour une boutique de vêtements en ligne. Nous allons générer 10 000 commandes simulées, chacune avec de nombreux détails (prix, client, logistique), et sauvegarder le tout dans un fichier CSV.
Étape 1 : Installation et import des bibliothèques
Nous commençons par installer Faker et importer toutes les bibliothèques nécessaires.
- pandas : Pour créer et manipuler notre tableau de données.
- Faker : Pour générer des données factices (noms, adresses, etc.).
- random, datetime, hashlib : Utilitaires Python pour l'aléatoire, la gestion des dates et la création d'identifiants uniques.
Python
# Installation de la bibliothèque Faker pour générer des données factices !pip install Faker
import pandas as pd import random import hashlib from datetime import datetime from faker import Faker
Étape 2 : Configuration des produits
C'est ici que nous définissons l'univers de notre boutique. Nous remplaçons les montres par des vêtements avec leurs propres caractéristiques, prix et images. Nous conservons la logique des périodes saisonnières pour simuler les soldes ou les collections d'hiver/été.
Python
# Initialisation de Faker fake = Faker()
PRODUCTS = { 'T-shirt Classique Blanc': { 'sku': 'TS-WH-CL-01', 'price': 25.00, 'price_ex_vat': 20.00, 'price_chf': 22.50, 'price_ex_vat_chf': 18.00, 'image': 'https://images.pexels.com/photos/428338/pexels-photo-428338.jpeg' }, 'Pull en Laine Hivernal': { 'sku': 'SW-GR-WO-01', 'price': 80.00, 'price_ex_vat': 64.00, 'price_chf': 72.00, 'price_ex_vat_chf': 57.60, 'image': 'https://images.pexels.com/photos/6770141/pexels-photo-6770141.jpeg' }, 'Jean Slim Noir - Premium': { 'sku': 'JN-BK-SL-PR', 'price': 120.00, 'price_ex_vat': 96.00, 'price_chf': 108.00, 'price_ex_vat_chf': 86.40, 'image': 'https://images.pexels.com/photos/1082529/pexels-photo-1082529.jpeg' }, 'Jean Slim Noir - Standard': { 'sku': 'JN-BK-SL-ST', 'price': 90.00, 'price_ex_vat': 72.00, 'price_chf': 81.00, 'price_ex_vat_chf': 64.80, 'image': 'https://images.pexels.com/photos/1598507/pexels-photo-1597507.jpeg' }, 'Robe d\'Été Fleurie': { 'sku': 'DR-FL-SU-01', 'price': 75.00, 'price_ex_vat': 60.00, 'price_chf': 67.50, 'price_ex_vat_chf': 54.00, 'image': 'https://images.pexels.com/photos/1755428/pexels-photo-1755428.jpeg' }, } PRODUCT_NAMES = list(PRODUCTS.keys())
CITIES = { 'Asia': ['Tokyo', 'Seoul', 'Shanghai', 'Beijing', 'Singapore'], 'Europe': ['Paris', 'London', 'Berlin', 'Rome', 'Madrid'], 'North America': ['New York', 'Los Angeles', 'Toronto'], 'Others': ['Sydney', 'Cairo', 'Moscow'] } COUNTRIES = ['CN', 'JP', 'KR', 'FR', 'UK', 'DE', 'IT', 'ES', 'US', 'CA', 'AU', 'EG', 'RU']
SEASONAL_PERIODS = [ (datetime(2023, 11, 15), datetime(2024, 1, 15)), (datetime(2024, 6, 20), datetime(2024, 8, 20)), (datetime(2024, 11, 15), datetime(2025, 1, 15)), ]
Étape 3 : Logique métier pour améliorer la qualité de la donnée
Ces fonctions ajoutent du réalisme. calculate_price_order ajuste le prix de vente final en fonction du produit et de la période. Par exemple, le Pull en Laine sera plus cher pendant les périodes de fêtes, simulant une forte demande hivernale.
Python
def generate_seasonal_date(): if random.random() < 0.5: start_date, end_date = random.choice(SEASONAL_PERIODS) return fake.date_time_between(start_date=start_date, end_date=end_date) return fake.date_time_between(start_date='-3y', end_date='now')
def calculate_price_order(name, product, creation_date): """Calcule le prix de vente final en fonction de règles métier.""" if 'T-shirt' in name: time_factor = (datetime.now() - creation_date).days / 365 sales_multiplier = 1 + random.uniform(0.2, 0.8) * time_factor return product['price'] * sales_multiplier * random.uniform(0.9, 1.1)
elif 'Pull en Laine' in name:
if any(start <= creation\_date <= end for start, end in SEASONAL\_PERIODS):
return product\['price'\] \* random.uniform(1.1, 1.3) # Plus cher en hiver
else:
return product\['price'\] \* random.uniform(0.9, 1.1)
elif 'Jean Slim' in name:
return product\['price'\] \* random.uniform(0.9, 1.1)
else: # Pour la robe et autres futurs articles
time\_factor = (datetime.now() - creation\_date).days / 365
sales\_reduction = max(1 - random.uniform(0.1, 0.5) \* time\_factor, 0.7)
return product\['price'\] \* sales\_reduction \* random.uniform(0.9, 1.0)
Étape 4 : Assemblage finale de la base de donnée
La fonction generate_row est le cœur du script. Elle construit une commande complète en piochant dans toutes nos configurations et en générant des dizaines de champs pour rendre chaque ligne unique et détaillée : informations client, paiement, logistique, etc.
Python
def generate_row(): creation_date = generate_seasonal_date() name = random.choice(PRODUCT_NAMES) product = PRODUCTS[name]
price\_order = calculate\_price\_order(name, product, creation\_date)
# Logique pour la localisation
region = 'Europe' if 'Jean' in name and random.random() < 0.5 else random.choice(list(CITIES.keys()))
city = random.choice(CITIES\[region\])
country\_code = random.choice(COUNTRIES)
row = {
'id': fake.unique.random\_int(min=1000000000, max=9999999999),
'creation\_date': creation\_date,
'update\_date': fake.date\_time\_between(start\_date=creation\_date, end\_date='now'),
'status': random.choices(\['complete', 'canceled', 'deleted'\], \[0.85, 0.1, 0.05\])\[0\],
'price\_order': round(price\_order, 2),
'traffic': random.randint(50, 500),
'city': city,
'country\_code': country\_code,
'total\_quantity': random.randint(1, 5),
'sku': product\['sku'\],
'name': name.capitalize(),
'price\_ex\_vat': product\['price\_ex\_vat'\],
'store\_name': fake.company(),
'price': product\['price'\],
'payment\_cc\_type': random.choice(\['VISA', 'MASTERCARD', 'AMEX'\]),
'quantity': random.randint(1, 5),
'quantity\_refunded': random.randint(0, 2),
'quantity\_invoiced': random.randint(1, 5),
'quantity\_returned': random.randint(0, 2),
'black\_friday': any(p\[0\].month == 11 and 20 < p\[0\].day < 30 for p in SEASONAL\_PERIODS if p\[0\] <= creation\_date <= p\[1\]),
'is\_customized': random.choices(\[True, False\], \[0.1, 0.9\])\[0\],
'discount': round(random.uniform(0, 25), 2),
'vat\_amount': round(price\_order \* 0.2, 2),
'options': random.choice(\[None, 'Emballage cadeau', 'Retouches simples'\]),
'subtotal\_invoiced': round(price\_order \* 0.8, 2),
'discount\_amount': round(price\_order \* random.uniform(0, 0.15), 2),
'tax\_invoiced': round(price\_order \* 0.2, 2),
'options\_id': random.choice(\['men', 'women', 'unisex'\]),
'payment\_base\_shipping\_amount': round(random.uniform(5, 15), 2),
'payment\_amount\_paid': round(price\_order, 2),
'order\_currency\_code': random.choice(\['USD', 'EUR', 'GBP'\]),
'coupon\_code': random.choice(\[None, 'SUMMER24', 'WINTER24', 'WELCOME10'\]),
'payment\_method': random.choice(\['paypal\_express', 'credit\_card'\]),
'shipping\_description': fake.country(),
'mail\_ID': fake.sha256(),
'shipping': random.choice(\['GLS', 'DHL', 'UPS'\]),
'payment': random.choice(\['paypal', 'credit card'\]),
'image': product\['image'\],
'price\_ex\_vat\_chf': product\['price\_ex\_vat\_chf'\],
'is\_combo': random.choices(\[True, False\], \[0.2, 0.8\])\[0\],
'discount\_chf': round(random.uniform(0, 20), 2),
'vat\_amount\_chf': round(product\['price\_chf'\] \* 0.077, 2),
'creation\_date\_local': fake.date\_time\_this\_year(),
'price\_chf': product\['price\_chf'\]
}
return row
print(generate_row())
Étape 5 : Export CSV
C'est l'étape finale ! Nous exécutons la fonction generate_row 10 000 fois pour créer notre jeu de données, nous le transformons en un tableau pandas (DataFrame), et nous le sauvegardons en fichier CSV.
Python
# Nombre de lignes à générer NUM_ROWS = 10000 print(f"Génération de {NUM_ROWS} lignes de données de commandes...")
rows = [generate_row() for _ in range(NUM_ROWS)]
df = pd.DataFrame(rows)
print("DataFrame généré avec succès !") print(df.head())
output_csv_path = 'fake_clothing_store_database.csv' df.to_csv(output_csv_path, index=False)
print(f"\nLes données ont été sauvegardées dans '{output_csv_path}'.") print("Vous pouvez trouver ce fichier dans le panneau de gauche de Colab (icône dossier).")
Le jeu de données généré par ce script est disponible pour consultation et téléchargement via ce lien Google Sheets.
➡️ **Base de Données E-commerce (Vêtements)
**Vous pouvez importer ces données sur un environnement Power BI Desktop ou Looker studio gratuitement afin de vous entraîner à la visualisation des données.
Code complet :
Python
# ==============================================================================
import pandas as pd import random import hashlib from datetime import datetime from faker import Faker
fake = Faker()
PRODUCTS = { 'T-shirt Classique Blanc': { 'sku': 'TS-WH-CL-01', 'price': 25.00, 'price_ex_vat': 20.00, 'price_chf': 22.50, 'price_ex_vat_chf': 18.00, 'image': 'https://images.pexels.com/photos/428338/pexels-photo-428338.jpeg' }, 'Pull en Laine Hivernal': { 'sku': 'SW-GR-WO-01', 'price': 80.00, 'price_ex_vat': 64.00, 'price_chf': 72.00, 'price_ex_vat_chf': 57.60, 'image': 'https://images.pexels.com/photos/6770141/pexels-photo-6770141.jpeg' }, 'Jean Slim Noir - Premium': { 'sku': 'JN-BK-SL-PR', 'price': 120.00, 'price_ex_vat': 96.00, 'price_chf': 108.00, 'price_ex_vat_chf': 86.40, 'image': 'https://images.pexels.com/photos/1082529/pexels-photo-1082529.jpeg' }, 'Jean Slim Noir - Standard': { 'sku': 'JN-BK-SL-ST', 'price': 90.00, 'price_ex_vat': 72.00, 'price_chf': 81.00, 'price_ex_vat_chf': 64.80, 'image': 'https://images.pexels.com/photos/1598507/pexels-photo-1597507.jpeg' }, 'Robe d\'Été Fleurie': { 'sku': 'DR-FL-SU-01', 'price': 75.00, 'price_ex_vat': 60.00, 'price_chf': 67.50, 'price_ex_vat_chf': 54.00, 'image': 'https://images.pexels.com/photos/1755428/pexels-photo-1755428.jpeg' }, } PRODUCT_NAMES = list(PRODUCTS.keys())
CITIES = { 'Asia': ['Tokyo', 'Seoul', 'Shanghai', 'Beijing', 'Singapore'], 'Europe': ['Paris', 'London', 'Berlin', 'Rome', 'Madrid'], 'North America': ['New York', 'Los Angeles', 'Toronto'], 'Others': ['Sydney', 'Cairo', 'Moscow'] } COUNTRIES = ['CN', 'JP', 'KR', 'FR', 'UK', 'DE', 'IT', 'ES', 'US', 'CA', 'AU', 'EG', 'RU']
SEASONAL_PERIODS = [ (datetime(2023, 11, 15), datetime(2024, 1, 15)), (datetime(2024, 6, 20), datetime(2024, 8, 20)), (datetime(2024, 11, 15), datetime(2025, 1, 15)), ]
def generate_seasonal_date(): if random.random() < 0.5: start_date, end_date = random.choice(SEASONAL_PERIODS) return fake.date_time_between(start_date=start_date, end_date=end_date) return fake.date_time_between(start_date='-3y', end_date='now')
def calculate_price_order(name, product, creation_date): """Calcule le prix de vente final en fonction de règles métier.""" if 'T-shirt' in name: time_factor = (datetime.now() - creation_date).days / 365 sales_multiplier = 1 + random.uniform(0.2, 0.8) * time_factor return product['price'] * sales_multiplier * random.uniform(0.9, 1.1)
elif 'Pull en Laine' in name:
if any(start <= creation\_date <= end for start, end in SEASONAL\_PERIODS):
return product\['price'\] \* random.uniform(1.1, 1.3) # Plus cher en hiver
else:
return product\['price'\] \* random.uniform(0.9, 1.1)
elif 'Jean Slim' in name:
return product\['price'\] \* random.uniform(0.9, 1.1)
else: # Pour la robe et autres futurs articles
time\_factor = (datetime.now() - creation\_date).days / 365
sales\_reduction = max(1 - random.uniform(0.1, 0.5) \* time\_factor, 0.7)
return product\['price'\] \* sales\_reduction \* random.uniform(0.9, 1.0)
def generate_row(): creation_date = generate_seasonal_date() name = random.choice(PRODUCT_NAMES) product = PRODUCTS[name]
price\_order = calculate\_price\_order(name, product, creation\_date)
region = 'Europe' if 'Jean' in name and random.random() < 0.5 else random.choice(list(CITIES.keys()))
city = random.choice(CITIES\[region\])
country\_code = random.choice(COUNTRIES)
row = {
'id': fake.unique.random\_int(min=1000000000, max=9999999999),
'creation\_date': creation\_date,
'update\_date': fake.date\_time\_between(start\_date=creation\_date, end\_date='now'),
'status': random.choices(\['complete', 'canceled', 'deleted'\], \[0.85, 0.1, 0.05\])\[0\],
'price\_order': round(price\_order, 2),
'traffic': random.randint(50, 500),
'city': city,
'country\_code': country\_code,
'total\_quantity': random.randint(1, 5),
'sku': product\['sku'\],
'name': name.capitalize(),
'price\_ex\_vat': product\['price\_ex\_vat'\],
'store\_name': fake.company(),
'price': product\['price'\],
'payment\_cc\_type': random.choice(\['VISA', 'MASTERCARD', 'AMEX'\]),
'quantity': random.randint(1, 5),
'quantity\_refunded': random.randint(0, 2),
'quantity\_invoiced': random.randint(1, 5),
'quantity\_returned': random.randint(0, 2),
'black\_friday': any(p\[0\].month == 11 and 20 < p\[0\].day < 30 for p in SEASONAL\_PERIODS if p\[0\] <= creation\_date <= p\[1\]),
'is\_customized': random.choices(\[True, False\], \[0.1, 0.9\])\[0\],
'discount': round(random.uniform(0, 25), 2),
'vat\_amount': round(price\_order \* 0.2, 2),
'options': random.choice(\[None, 'Emballage cadeau', 'Retouches simples'\]),
'subtotal\_invoiced': round(price\_order \* 0.8, 2),
'discount\_amount': round(price\_order \* random.uniform(0, 0.15), 2),
'tax\_invoiced': round(price\_order \* 0.2, 2),
'options\_id': random.choice(\['men', 'women', 'unisex'\]),
'payment\_base\_shipping\_amount': round(random.uniform(5, 15), 2),
'payment\_amount\_paid': round(price\_order, 2),
'order\_currency\_code': random.choice(\['USD', 'EUR', 'GBP'\]),
'coupon\_code': random.choice(\[None, 'SUMMER24', 'WINTER24', 'WELCOME10'\]),
'payment\_method': random.choice(\['paypal\_express', 'credit\_card'\]),
'shipping\_description': fake.country(),
'mail\_ID': fake.sha256(),
'shipping': random.choice(\['GLS', 'DHL', 'UPS'\]),
'payment': random.choice(\['paypal', 'credit card'\]),
'image': product\['image'\],
'price\_ex\_vat\_chf': product\['price\_ex\_vat\_chf'\],
'is\_combo': random.choices(\[True, False\], \[0.2, 0.8\])\[0\],
'discount\_chf': round(random.uniform(0, 20), 2),
'vat\_amount\_chf': round(product\['price\_chf'\] \* 0.077, 2),
'creation\_date\_local': fake.date\_time\_this\_year(),
'price\_chf': product\['price\_chf'\]
}
return row
NUM_ROWS = 10000 print(f"Génération de {NUM_ROWS} lignes de données de commandes...")
rows = [generate_row() for _ in range(NUM_ROWS)]
df = pd.DataFrame(rows)
print("\nDataFrame généré avec succès ! Voici les 5 premières lignes :") print(df.head())
output_csv_path = 'fake_clothing_store_database.csv' df.to_csv(output_csv_path, index=False)
print(f"\nLes données ont été sauvegardées dans le fichier '{output_csv_path}'.")