2013-06-24 141 views
17

我想根据它们的值对百分比桶中的数据进行分类。我的数据看起来像,在熊猫中创建百分比桶

a = pnd.DataFrame(index = ['a','b','c','d','e','f','g','h','i','j'], columns=['data']) 
a.data = np.random.randn(10) 
print a 
print '\nthese are ranked as shown' 
print a.rank() 

     data 
a -0.310188 
b -0.191582 
c 0.860467 
d -0.458017 
e 0.858653 
f -1.640166 
g -1.969908 
h 0.649781 
i 0.218000 
j 1.887577 

these are ranked as shown 
    data 
a  4 
b  5 
c  9 
d  3 
e  8 
f  2 
g  1 
h  7 
i  6 
j 10 

要排名这个数据,我使用排名函数。不过,我有兴趣创造一个前20%的桶。在上面所示的例子中,这将是包含标签[“C”,“J”]

desired result : ['c','j'] 

列表如何获得所需的结果

回答

24
In [13]: df[df > df.quantile(0.8)].dropna() 
Out[13]: 
     data 
c 0.860467 
j 1.887577 

In [14]: list(df[df > df.quantile(0.8)].dropna().index) 
Out[14]: ['c', 'j']