Using Pandas to calculate the percentage of each index's values

Question:

I get stuck in pandas when I have a DataFrame like this:

Person Fruit
A Apple
A Banana
A Banana
B Apple
C Banana
A Apple
F Banana
D Banana

I need to calculate the percentage of each person’s different fruits, like A has 4 fruits, which will return 50% Apple and 50% Banana. D has 1 fruit, which will return 100% Banana.

Is there any method that I can achieve this result?

Thank you so much for help!

Asked By: J Lee

||

Answers:

You can use pandas.Series.value_counts :

result = (
            (df.groupby('Person')['Fruit']
               .value_counts(normalize=True))
               .astype(float)
               .map("{:.2%}".format)
          )

# Output :

print(result)

Person  Fruit 
A       Apple      50.00%
        Banana     50.00%
B       Apple     100.00%
C       Banana    100.00%
D       Banana    100.00%
F       Banana    100.00%
Name: Fruit, dtype: object
Answered By: abokey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.