使用类似这样的函数:
def percentage(part, whole):
return 100 * float(part)/float(whole)
我可以获得调用函数的百分比:
percentage(215,413)
但是我如何获取 bool 系列并获得 True 和 False 的百分比?
s = pd.Series(np.random.randn(5) +5)
s > 5
0 True
1 False
2 False
3 True
4 True
dtype: bool
所需输出是系列中真值的百分比(上例中为 60%)。我需要 map 一个函数来获取输出吗?请告诉我最好的方法。谢谢
请您参考如下方法:
只需计算 bool pandas 系列的平均值。由于 True 映射到 1,False 映射到 0,您最终将通过简单地计算平均值来获得百分比。:
import pandas as pd, numpy as np
s = pd.Series(np.random.randn(5) +5)
(s > 5).mean()
0.59999999999999998
