Skip to content
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_excel(r"Case_Study.xlsx", sheet_name=['Satış Verisi', 'BÖLGE LİSTESİ'])
df["Satış Verisi"]
df["BÖLGE LİSTESİ"]
merged_df = pd.merge(df["Satış Verisi"], df["BÖLGE LİSTESİ"], on='BÖLGE KODU'); merged_df
merged_df['BAŞARI YÜZDESİ'] = merged_df['PRİM'] / merged_df['HEDEF']
merged_df
merged_df.rename(columns={
    'SATIŞ TEMSİLCİSİ': 'SalesRepresentative',
    'BÖLGE KODU': 'RegionCode',
    'YIL': 'Year',
    'AY': 'Month',
    'BRANŞ': 'Branch',
    'PRİM': 'Bonus',
    'HEDEF': 'Target',
    'BAŞARI YÜZDESİ': 'SuccessPercentage',
    'BÖLGE ADI': 'RegionName'
}, inplace=True)
merged_df
merged_df[["Bonus", "Target", "SuccessPercentage"]].describe()
merged_df.Year.unique()
year_name = int(merged_df.Year.unique()); year_name
monthly_totals = merged_df.groupby(['SalesRepresentative', 'Year', 'Month'])[['Bonus', 'Target']].sum().reset_index()
monthly_totals['MonthlyRatio'] = monthly_totals['Bonus'] / monthly_totals['Target']
monthly_totals
monthly_totals.set_index(['SalesRepresentative','Month'])
branch_totals = merged_df.groupby(['SalesRepresentative', 'Branch'])[['Bonus', 'Target']].sum().reset_index()
branch_totals['BranchRatio'] = monthly_totals['Bonus'] / monthly_totals['Target']
branch_totals
branch_totals.set_index(['SalesRepresentative','Branch'])
# Belirlediğim boyutlarda bar chart oluşturuyorum
plt.figure(figsize=(14, 8))

# Satış temsilcileri isimlerini ve ayları unique olarak çekiyorum. 
# Çünkü x ve y axis'lerinin isimlendirilmelerinde kullanacağım.
sales_representatives = monthly_totals['SalesRepresentative'].unique()
months = monthly_totals['Month'].unique()

# Her ay için bir renk assign ediyorum.
colors = {
    1: 'tab:orange',
    2: 'tab:blue',
    3: 'tab:red',
    4: 'tab:green',
    5: 'tab:purple'
}

# Şimdi chart'ı data ile create ediyorum
bar_width = 0.15
for i, month in enumerate(months):
    month_data = monthly_totals[monthly_totals['Month'] == month]
    bar_positions = [x + (i * bar_width) for x in range(len(month_data))]
    plt.bar(bar_positions, month_data['MonthlyRatio'], color=colors[month], width=bar_width, label=f'{month}')

# x axis'ini belirliyorum
xticks_positions = []
for r in range(len(sales_representatives)):
    xticks_positions.append(r + bar_width)
plt.xticks(xticks_positions, sales_representatives, rotation=45)

# y axis'inin parametrelerini belirliyorum. Kaçtan kaça gidecek ve hangiaralıklar ile gidecek. 
plt.ylim(0, 3)
plt.yticks([i * 0.1 for i in range(31)])

plt.title(f'Monthly Target Achievement Performance of Sales Representatives in {year_name}')
plt.xlabel('Sales Representatives')
plt.ylabel('MonthlyRatio (Bonus/Target Ratio)')
plt.legend(title='Months')
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()