问题
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