Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算熊猫的百分位数_Python_Pandas - Fatal编程技术网

Python 计算熊猫的百分位数

Python 计算熊猫的百分位数,python,pandas,Python,Pandas,我有一个名为join2的数据集,如下所示 pd.DataFrame({'id' : [197, 220, 278, 300, 303, 318, 326, 339, 354, 382, 407, 432, 433, 440, 441, 447, 454, 501, 504, 508, 550, 564,601, 602, 606,628,643, 668,688,718], 'count' : [10, 5, 5, 5,15, 5, 5, 25, 10, 5, 5, 5, 20, 15,

我有一个名为join2的数据集,如下所示

   pd.DataFrame({'id' : [197, 220, 278, 300, 303, 318, 326, 339, 354, 382, 407, 432, 433, 440, 441, 447, 454, 501, 504, 508, 550, 564,601, 602, 606,628,643, 668,688,718], 'count' : [10, 5, 5, 5,15, 5, 5, 25, 10, 5, 5, 5, 20, 15, 5, 5, 10, 10, 10, 5, 5,5,5, 5,10,10,5, 10, 15, 5]
, 'sum' : [6, 3, 5, 3, 11, 1, 4, 13, 4, 3, 1, 5, 16, 9, 1, 5, 8, 10, 10, 4, 5, 5, 5, 4, 6, 10, 1, 6, 15, 5],
'percentage' : [60.0,60.0,100.0,60.0,73.33333333333333,20.0,80.0,52.0,40.0,60.0,20.0,100.0,80.0,60.0,20.0,100.0,80.0,100.0,100.0,80.0,100.0,100.0, 100.0,80.0, 60.0, 100.0, 20.0, 60.0, 100.0, 100.0]})

我想添加一个名为percentile的新列

这两种我都试过了


    join2['pctile'] = join2['percentage'].rank(pct=True)
and 
    sz = join2['percentage'].size-1
    join2['pctile'] = join2['percentage'].rank(method='max').apply(lambda x: 100.0*(x-1)/sz)

但是我得到的百分位数是不正确的。百分比应为25%,其中百分比为60%。我如何解决这个问题?

您要查找的是
数据帧.分位数()

为70%分位数

df1.quantile(0.7)

使用此
方法='average'
而不是
'method='min'

df['pctile'] = df['percentage'].rank(method='average').apply(lambda x: 100.0*(x-1)/sz)
输出:

>>> df
    id  count  sum  percentage  pctile
0  197     10    6          60    25.0
1  220      5    3          60    25.0
2  278      5    5         100   100.0
3  300      5    3          60    25.0
4  303     15   11          73    75.0

单击此处查看
.rank()

所有行的预期输出文档?我不明白为什么百分比应该是25:60percentage@jezrael我想在一个新的列中的percentage列中的每个值的百分位数,即pctileYes,那么预期的数字是什么呢?预期的数字是这样的-25%代表60%,100%代表100%,除非必要,否则cavoid图像可以帮助人们在手机上浏览,不希望浪费数据,而大图像会妨碍可读性。增加建议解决方案的返回值的想法很好-只是不要作为图像来做;将其作为代码格式的文本发布。通常人们会将其作为代码注释发布,然后它会变灰,因此可读,但在解决方案本身的背景中有一点。我已经更新了我的数据帧。请检查您在此给出的解决方案,因为它在此数据集中没有给出正确的o/p。答案看起来正确,您能指出预期的输出吗?当百分比为100时,您是否期望百分比为100?是的。但是,当我在更新的数据集中应用它时,它并没有给出期望的输出。您所做的正是我要查找的,但要查找这些更新的数据。我建议您创建3列,
df['pctile\u min']
df['pctile\u avg']
df['pctile\u max']
,使用
method='min'
method='average'
method='max'
分别查看哪组结果最适合您所寻找的结果。您对百分位数的理解可能与传统理解略有不同。如果按照维基百科中的定义,您应该使用
method='min'
>>> df
    id  count  sum  percentage  pctile
0  197     10    6          60    25.0
1  220      5    3          60    25.0
2  278      5    5         100   100.0
3  300      5    3          60    25.0
4  303     15   11          73    75.0