Skip to content

Instantly share code, notes, and snippets.

@ttpro1995
Created April 4, 2021 03:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttpro1995/5b1d0dae869c97ff94ebc7ab694d0cb7 to your computer and use it in GitHub Desktop.
Save ttpro1995/5b1d0dae869c97ff94ebc7ab694d0cb7 to your computer and use it in GitHub Desktop.
describe_category with percentage
def describe_category(dataframe, column_name):
"""
plot describe category with percentage
"""
value_count = dataframe[column_name].value_counts().sort_index()
df_value_count = pd.DataFrame({column_name: value_count.index, "count": value_count.values})
sum_class = df_value_count["count"].sum()
df_value_count["percentage"] = df_value_count["count"]/sum_class*100
display(df_value_count)
# fig, ax = plt.subplots(figsize = (1,7)
ax = sns.barplot(data=df_value_count, x=column_name, y="count")
ax.set_ylim(0, df_value_count["count"].max()*1.2)
for p, percentage in zip(ax.patches, list(df_value_count["percentage"])):
ax.annotate("%.2f" % percentage +" %", (p.get_x() + p.get_width() / 2., p.get_height()),
ha='center', va='center', rotation=0, xytext=(0, 20), textcoords='offset points') #vertical bars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment