test={'price':[1,3,4,2,1,2,1,6]}
test=pd.DataFrame(test)
test=test.sort_values('price', ascending=False)
sum_test=test.sum()
test['percentage']=(test/sum_test)*100
我想要做的是计算总和为50%的百分比列中的值数量。唯一的条件是价格栏必须按降序排序。在提供的示例数据中,正确的输出为2.熊猫计数值占总数的50%
最初我正在用count计算条件,但是我到了死胡同。
test['percentage'].count().where(test['percentage'].sum()<=50,0)
不过,我得到以下错误: “numpy.int32”对象有没有属性“其中” 任何关于我哪里错了的想法?
干杯,布兰登
您的操作顺序是错误的。 'test ['percentage']。count()'统计“百分比”列中的所有项目。没有从那里去的地方。 –