将count方法的输出转换为百分比值

时间:2016-07-01 08:45:39

标签: python pandas

如果我在对象上运行.count():

animals = all_vals[['Dog','Cat','Mouse','Snake']].count()

并收到此输出:

animals
Out[49]:
Dog      173
Cat      13
Mouse    42
Snake    359
dtype: int64

如何获得每只动物的百分比?我可以将.count的输出发送到另一种方法吗?

1 个答案:

答案 0 :(得分:3)

您可以使用:

print (animals)
Dog      173
Cat       13
Mouse     42
Snake    359
Name: animals, dtype: int64

print (100 * animals / animals.sum())
Dog      29.471891
Cat       2.214651
Mouse     7.155026
Snake    61.158433
Name: animals, dtype: float64