plot a pie chart based on the percentage of occurrences of a string in a specific column of a pandas data frame

主宰稳场 提交于 2020-03-06 09:29:01

问题


Consider a data frame like this:

import pandas as pd

df = pd.DataFrame(["man", "woman", "man", "other", "other", "man", "woman"], columns=["gender"])

now I want to have a pie chart with matplotlib or seaborn, or any other Python plotting library for that matter, which shows the percentages of the occurrence of any of the values man, woman, other to the total size as the slices of the pie. Basically for the above example

men: 3/7
woman: 2/7
other: 2/7

should be the size of the slices of the pie. If I could group the data frame based on the string value of that specific column and then store the number of occurrences in a different column, then I could use the example on this page to achieve what I'm looking for. I tried the df.groupby("gender") and df.groupby("gender").count() from this page with no avail.

来源:https://stackoverflow.com/questions/60233858/plot-a-pie-chart-based-on-the-percentage-of-occurrences-of-a-string-in-a-specifi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!