Skip to content

Instantly share code, notes, and snippets.

@VincentTatan
Last active November 22, 2020 08:24
Show Gist options
  • Save VincentTatan/19911d4a8ddcb10702cf499ddc11e3bf to your computer and use it in GitHub Desktop.
Save VincentTatan/19911d4a8ddcb10702cf499ddc11e3bf to your computer and use it in GitHub Desktop.
def fixed_width_cut(df,feature,labels=['Low','Medium','High']):
feature_slice, retbins = pd.cut(df[feature], len(labels) ,retbins=True, labels=labels)
retbins = [ '%.2f' % elem for elem in retbins ]
return feature_slice,retbins
def quartile_cut(df,feature,labels=['Low','Medium','High']):
feature_slice, retbins = pd.qcut(df[feature], q=len(labels),retbins=True,labels=labels)
retbins = [ '%.2f' % elem for elem in retbins ]
return feature_slice,retbins
# Specified cuts
salary_df['average_monthly_hours_bin'], retbins = fixed_width_cut(salary_df,'average_montly_hours',labels=['Low','Medium','High'])
salary_df['average_monthly_hours_qbin'], qretbins = quartile_cut(salary_df,'average_montly_hours',labels=['Low','Medium','High'])
# Visualize
draw_plot(salary_df,['average_monthly_hours_bin','average_monthly_hours_qbin'],'Projects related (Left= fixed width cut, right = quartile cut)',type='countplot')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment