![](/img/trans.png)
[英]How to get the top 5 percentile values in pandas series for each class?
[英]How to get the Cumulative percentile value of each row in a Python time series
如何获得累积百分位值?
Dates
1990-01-02 17.24
1990-01-03 18.19
1990-01-04 19.22
1990-01-05 20.11
1990-01-08 20.26
1990-01-09 22.20
1990-01-10 22.44
1990-01-11 20.05
1990-01-12 24.64
1990-01-15 26.34
1990-01-16 24.18
2行数据中第2行的百分位数和3行数据中第3行的百分位数等等?
你可以这样做:
import pandas as pd
import numpy as np
df=pd.read_excel('filename.xlsx') #replace filename with name of your excel file
df['date']=pd.to_datetime(df['date']) #this doesn't affect your percentile calculation but you do it to leverage full power of pandas datetime functions
val_list=df.val.values
vals=[]
perc=[]
for r in range(len(val_list)):
l=[x for x in val_list[0:r+1]]
vals.append(l)
for value in vals:
perc.append(np.percentile(value,50)) #change 50 to the percentile you want to calculate
df['percentile']=perc
print(df)
这里需要注意的几个关键点:
1) 我通过将您的数据导入为 pandas DF 来完成计算。 如果您想在 numpy 数组本身中执行此操作,则对上述代码进行一些调整即可。 但是,Pandas DF 是查看 Python 中表格数据的一种优雅方式。
2)这可能不是最有效的方法,但它可以完成工作。 因此,在非常大的数据集上小心使用它。
3)研究代码中提到的注释。
希望这可以帮助。 如果它没有在下面的评论中回复,我会尝试对其进行整理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.