Created
March 27, 2020 14:22
-
-
Save Mgmaplus/c74090f83f59c500ca817c1ae24ef7e3 to your computer and use it in GitHub Desktop.
Barplot for #datacamplive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Merge dataframes | |
all_sales_data = sales_date_customer.merge(employees_data, on = 'EmployeeName', how = 'left') | |
#New column CommissionEarned | |
all_sales_data['SalesCommission'] = all_sales_data['TotalDue'] * all_sales_data['CommissionPct'] | |
#Grouping results by employee and year | |
commission_per_employee_per_year = all_sales_data.groupby(['OrderYear','EmployeeName']).sum() | |
#Sort and select with loc year 2012 results | |
commission_per_employee_per_year.sort_values(by='SalesCommission', ascending = True, inplace = True) | |
commission_leaders_2012 = commission_per_employee_per_year.loc[(2012)] | |
#Visualize | |
fig, ax = plt.subplots(figsize= (18,6)) | |
sns.set_color_codes('pastel') | |
ax = sns.barplot(commission_leaders_2012.index, 'SalesCommission', data = commission_leaders_2012, palette= 'Blues') | |
ax.set_xlabel('Employee') | |
ax.set_ylabel('Sales Commission USD') | |
ax.set_title('Earned Commission per Employee in 2012 (With Commission% earned)') | |
ax.set_xticklabels(commission_leaders_2012.index, rotation = 45) | |
#Set up commission pct for our 2012 leader by merging with employees original df | |
commission_leaders_2012 = commission_leaders_2012.merge(employees_data[['CommissionPct', 'EmployeeName']], on = 'EmployeeName', how = 'left') | |
#Annotate CommissionPct earned on every employee | |
z = 0 | |
for i in ax.patches: | |
ax.text(i.get_x()-.03, i.get_height()+.5, \ | |
commission_leaders_2012['CommissionPct_y'][z], fontsize=15, | |
color='dimgrey') | |
z += 1 | |
plt.show() | |
#Datacamp is something special! | |
fig.savefig('Bestpaid_employees_2012.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment