0
我有CSV文件:如何从频率表中计算百分位数?
fr id
1 10000152
1 10000212
1 10000847
1 10001018
2 10001052
2 10001246
14 10001908
...........
这是一个频率表,其中id
是整型变量,而fr
是给定值的出现次数。文件按值按升序排列。 我想计算百分位数(即90%,80%,70%... 10%)的变量。
我在纯Python做到了这一点,类似这样的伪代码:
bucket=sum(fr)/10.0
percentile=1
sum=0
for (current_fr, current_id) in zip(fr,id):
sum=sum+current_fr
if (sum > percentile*bucket):
print "%i percentile: %i" % (percentile*10,current_id)
percentile=percentile+1
但这种代码是很原始:它没有考虑到百分应值之间的距离设置,它可以不退步等
有没有更优雅的,通用的,现成的解决方案?