简体   繁体   中英

how to calculate percentile of each 15 rows in python

I have 810 rows in my data frame about vehicle speed and I was trying to calculate the 85th percentile speed for each 15 rows. Here what I did so far:

count = 0
stat1 = []
for i, row in df.iterrows():
    if count == 10:
        stat1 = df.dbl_v.quantile(0.85)
        count = 0
    else:
        count += 1

how can I store and see the 85th percentile speed? It's supposed to be there are 54 of 85th percentile speed.

Thankyou

We can create groups every 15 lines using the remainder of the division by 15

grouper = df.groupby(df.index // 15)

To create a new DF by applying .quantile to multiple columns, we can do this:

df_new = grouper.agg(
               quantile85 = ('dbl_v', lambda x: x.quantile(0.85))
              ,quantile20 = ('other_column', lambda x: x.quantile(0.20))
              ,mean_dbl_v = ('dbl_v', 'mean')
              ,sum_col3 = ('name_column3', 'sum')
               #and so on...
           )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM