14-10 분위수 (quantile)
DataFrame.quantile(q=0.5, axis=0, numeric_only=True, interpolation='linear')
개요
quantile메서드는 해당 행/열의 분위수의 해당하는 값을 반환하는 메서드입니다.
사용법
기본 사용법
※ 자세한 내용은 아래 예시를 참고 바랍니다.
df.quantile(q=0.5, axis=0, numeric_only=True, interpolation='linear')
q : 분위수 입니다. 소수로 표현합니다. (예 : 75% = 0.75)
aixs : 분위수의 값을 구할 축입니다.
numeric_only : 수(소수)만 대상으로할지 여부입니다. False일 경우 datetime 및 timedelta 데이터의 분위수도 계산됩니다.
interpolation : 분위수에 값이 없을때 보간하는 방법입니다. 방식은 아래와 같습니다.
liner : i + (j - i) x 비율 [분위수 앞, 뒤 수 간격 * 비율]
lower : i [분위수 앞, 뒤수 중 작은수]
higher : j [분위수 앞, 뒤수 중 큰수]
midpoint : (i+j)÷2 [분위수 앞, 뒤수의 중간값]
nearest : i or j [분위수 앞, 뒤수중 분위수에 가까운 수]
예시
먼저 기본적인 사용법 예시를 위해 3x3짜리 객체를 생성하겠습니다.
idx = ['row1','row2','row3']
col = ['col1','col2','col3']
data= [[0,1,32],[50,10,-9],[100,100,18]]
df = pd.DataFrame(data,idx,col)
print(df)
>>
col1 col2 col3
row1 0 1 32
row2 50 10 -9
row3 100 100 18
기본적인 사용법
q의 값에 따라 해당 분위수를 출력하게 됩니다.
0분위수의 경우
print(df.quantile(q=0))
# 0분위수인 가장 작은수를 출력
>>
col1 0.0
col2 1.0
col3 -9.0
Name: 0.0, dtype: float64
0.5분위수의 경우
print(df.quantile(q=0.5))
# 0.5분위수인 중간값을 출력
>>
col1 50.0
col2 10.0
col3 18.0
Name: 0.5, dtype: float64
1분위수의 경우
print(df.quantile(q=1))
# 1분위수인 가장 큰 값 출력
>>
col1 100.0
col2 100.0
col3 32.0
Name: 1.0, dtype: float64
interpolation인수의 사용
interpolation은 분위수의 해당하는 값이 없는 경우 어떤 수를 출력할지 정하는 보간법입니다.
linear의 경우 i + (j - i) x 비율입니다.
print(df.quantile(q=0.75, interpolation='linear'))
>>
col1 75.0
col2 55.0
col3 25.0
Name: 0.75, dtype: float64
lower의 경우 i [분위수 앞, 뒤수 중 작은수] 입니다.
print(df.quantile(q=0.75, interpolation='lower'))
>>
col1 50
col2 10
col3 18
Name: 0.75, dtype: int64
higher의 경우 j [분위수 앞, 뒤수 중 큰수] 입니다.
col1 100
col2 100
col3 32
Name: 0.75, dtype: int64
midpoint의 경우 (i+j)÷2 [분위수 앞, 뒤수의 중간값] 입니다.
print(df.quantile(q=0.75, interpolation='midpoint'))
>>
col1 75.0
col2 55.0
col3 25.0
Name: 0.75, dtype: float64
nearest의 경우 i or j [분위수 앞, 뒤수중 분위수에 가까운 수] 입니다.
print(df.quantile(q=0.75, interpolation='nearest'))
>>
col1 100
col2 100
col3 32
Name: 0.75, dtype: int64
'파이썬완전정복-Pandas DataFrame > 14. 통계(기초)' 카테고리의 다른 글
Pandas DataFrame 14-09 누적합/누적곱 (cumsum / cumprod) (0) | 2022.02.07 |
---|---|
Pandas DataFrame 14-08 누적 최대/최소 (cummax / cummin) (0) | 2022.02.07 |
Pandas DataFrame 14-07 평균절대편차 (mad) (0) | 2022.02.07 |
Pandas DataFrame 14-06 분산 (var) (0) | 2022.02.07 |
Pandas DataFrame 14-05 표준편차 (std) (0) | 2022.02.07 |