Home > front end >  Using Pandas to calculate the percentage of each index's values
Using Pandas to calculate the percentage of each index's values

Time:10-02

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!

CodePudding user response:

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
  • Related