假设我们有以下框架和程序逻辑
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'B': [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]})
def more_than(series, threshold=5):
try:
trues = series.value_counts()[True]
p = trues / len(series) * 100
except KeyError:
p = 0
return True if p > threshold else False
df['compare'] = df['A'] > 5
print(more_than(df['compare']))
# True here
我希望具有类似于all(...)
的功能,但可能有阈值(如上所述).它可以正常工作,但我想知道这里是否有内置的东西,而且可能更快.