我有一个pandas数据框my_df,在那里我可以找到给定列的mean(),median(),mode():
my_df['field_A'].mean()
my_df['field_A'].median()
my_df['field_A'].mode()
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能找到更详细的统计数据,如90%?谢谢!
我想创建一个函数,它将(有序)列表作为其参数,并输出一个包含每个元素的相应百分位数的列表.
例如,fn([1,2,3,4,17])退货[0.0, 0.25, 0.50, 0.75, 1.00].
任何人都可以请:
我目前的代码:
def median(mylist):
length = len(mylist)
if not length % 2:
return (mylist[length / 2] + mylist[length / 2 - 1]) / 2.0
return mylist[length / 2]
###############################################################################
# PERCENTILE FUNCTION
###############################################################################
def percentile(x):
"""
Find the correspoding percentile of each value relative to a list of values.
where x is the list of values
Input list should already be sorted!
"""
# sort the input list …Run Code Online (Sandbox Code Playgroud) 我的程序中有一个字典,每个值都是响应时间列表.我需要计算每个列表的95百分位响应时间.我知道如何计算平均值,但不知道95百分位计算.任何指针将不胜感激.
以下是我的程序的字典输出
finalvalues = {' https://lp1.soma.sf.com/img/chasupersprite.qng?v=182-4':['505','1405','12','12','3 '] ,' https : //lp1.soma.sf.com/img/metaBar_sprite.dsc ':['154','400','1124','82','94','108']}
我想从 Python 中的多个大向量的集合中计算百分位数。有没有更有效的方法,而不是尝试连接向量,然后通过numpy.percentile将生成的巨大向量放入?
我的想法是,首先,计算不同值的频率(例如使用scipy.stats.itemfreq),其次,将不同向量的项目频率组合起来,最后,根据计数计算百分位数。
不幸的是,我无法找到用于组合频率表的函数(它不是很简单,因为不同的表可能涵盖不同的项目),或者用于从项目频率表计算百分位数。我需要实现这些,还是可以使用现有的 Python 函数?这些功能是什么?
我在这里和这里偶然发现了用于计算百分位数的纯python实现:
import math
import functools
def percentile(N, percent, key=lambda x:x):
"""
Find the percentile of a list of values.
@parameter N - is a list of values. Note N MUST BE already sorted.
@parameter percent - a float value from 0.0 to 1.0.
@parameter key - optional key function to compute value from each element of N.
@return - the percentile of the values
"""
if not N:
return None
k = (len(N)-1) * percent
f …Run Code Online (Sandbox Code Playgroud) Students=['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10']
Marks = [45, 78, 12, 14, 48, 43, 47, 98, 35, 80]
def display_dash_board(students, marks):
dictionary = dict(zip(Students,Marks))
# write code for computing top top 5 students
print("Top 5 Students are :\n\n")
for key, value in sorted(dictionary.items(), key=lambda item: item[1],reverse=True)[:5]:
print("%s: %s" % (key, value))
# write code for computing top least 5 students
print("\n\n Top Least 5 Students are : \n\n")
for key, value in sorted(dictionary.items(), key=lambda item: item[1])[:5]:
print("%s: %s" % (key, value))
# write code for …Run Code Online (Sandbox Code Playgroud) 我用这个函数从这里计算百分位数:
import numpy as np
a = [12, 3, 45, 0, 45, 47, 109, 1, 0, 3]
np.percentile(a, 25)
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
AttributeError: 'module' object has no attribute 'percentile'
Run Code Online (Sandbox Code Playgroud)
我也试过了
import numpy.percentile as np
Run Code Online (Sandbox Code Playgroud)
但它没有我得到同样的错误.
我的numpy版本是1.3.0我试图升级,但似乎我不会使用:[sudo pip install --upgrade scipy][2]但我发现没有升级.
我的ubuntu版本9.10
我的python版本是:2.6.4
我也试图绕过numpy.percentile模块,我在这里找到了:
>>> def percentile(N, P):
... n = int(round(P * len(N) + 0.5))
... if n > 1:
... return N[n-2]
... else:
... return 0
...
>>> a = [1, 23, …Run Code Online (Sandbox Code Playgroud) python ×8
numpy ×4
percentile ×4
scipy ×3
median ×2
lambda ×1
pandas ×1
python-2.7 ×1
statistics ×1