pandas quantile

pandas quantile

Guide

basic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""
for array of length n:
1. pos = 1 + (n-1)*p
2. get integer part and fraction part of pos: i, f
3. return a[i-1] + (a[i]-a[i-1])*f
"""
import math
def quantile_p(data, p, method=1):
data.sort()
if method == 2:
pos = 1 + (len(data)-1)*p
else:
pos = (len(data) + 1)*p
i = int(math.modf(pos)[1])
f = pos - i
Q = data[i-1] + (data[i]-data[i-1])*f
Q1 = quantile_p(data, 0.25)
Q2 = quantile_p(data, 0.5)
Q3 = quantile_p(data, 0.75)
IQR = Q3 - Q1
return Q1, Q2, Q3, IQR

demo

1
2
3
4
5
6
7
8
import pandas as pd
import numpy as np
df = pd.Series(np.array([6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36])

print(dt)
print('Q1:', df.quantile(.25))
print('Q2:', df.quantile(.5))
print('Q3:', df.quantile(.75))

pandas use method 2: pos = 1 + (n-1)*p

Reference

History

  • 20190524: created.
赞赏