LoginSignup
1
0

More than 1 year has passed since last update.

Python勉強⑤ Pandas quantile 2020/06/22

Last updated at Posted at 2022-06-23

q分位数(q-quantile)とは分布をq:1-qに分割する値。
q/100分位数をqパーセンタイルという。(Wikipedia)

例えば25%タイルが欲しければquantile(0.25)として指定する。

記憶しやすいようdatetimeと合わせてダイエット記録(フィクション)をpandasでいじってみる。

import pandas as pd

df = pd.DataFrame({'weight': range(50, 60),
                   'fat': range(25, 35),
                   'date': range(1, 11)})



df['date'] = pd.date_range('2019-01-01', '2019-01-10')

print(df)

出力

   weight  fat       date
0      50   25 2019-01-01
1      51   26 2019-01-02
2      52   27 2019-01-03
3      53   28 2019-01-04
4      54   29 2019-01-05
5      55   30 2019-01-06
6      56   31 2019-01-07
7      57   32 2019-01-08
8      58   33 2019-01-09
9      59   34 2019-01-10

体重も体脂肪も微増していく悲しい記録。

何も指定しなけば50%タイル値になる

print(df.quantile())

weight    54.5
fat       29.5
Name: 0.5, dtype: float64

numeric_onlyをFalseにすることで数値ではないdatetime型にも適用される。まだ1/5はマシだったのに...

print(df.quantile(numeric_only=False))

weight                   54.5
fat                      29.5
date      2019-01-05 12:00:00
Name: 0.5, dtype: object

20%タイルの時点で食生活を改めていれば...

print(df.quantile(0.2))

weight    51.8
fat       26.8
Name: 0.2, dtype: float64

リストで指定することで各パーセンタイルを一度に指定することも可能。

print(df.quantile([0, 0.25, 0.5, 0.75, 1.0]))

      weight    fat
0.00   50.00  25.00
0.25   52.25  27.25
0.50   54.50  29.50
0.75   56.75  31.75
1.00   59.00  34.00

さぁ今日からヘルシーにいこ。

参考サイト
note.nkmk
https://note.nkmk.me/python-pandas-quantile/

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0