パンダ: 特定のパーセンタイルで description() を使用する方法


description()関数を使用すると、pandas DataFrame 内の変数の記述統計を生成できます。

デフォルトでは、pandas は変数の 25、50、および 75 パーセンタイルを計算します。

ただし、 describe()関数でパーセンタイル引数を使用すると、計算する正確なパーセンタイルを指定できます。

次の例は、実際に次の pandas DataFrame でこの引数を使用する方法を示しています。

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   ' points ': [18, 22, 19, 14, 14, 11, 20, 28],
                   ' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
                   ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print (df)

  team points assists rebounds
0 A 18 5 11
1 B 22 7 8
2 C 19 7 10
3 D 14 9 6
4 E 14 12 6
5 F 11 9 5
6 G 20 9 9
7:28 4 12

例 1: デフォルトのパーセンタイルでの description() の使用

次のコードは、 describe()関数を使用して、DataFrame 内の各数値変数の記述統計を計算する方法を示しています。

 #calculate descriptive statistics for each numeric variable
df. describe ()

points assists rebounds
count 8.000000 8.00000 8.000000
mean 18.250000 7.75000 8.375000
std 5.365232 2.54951 2.559994
min 11.000000 4.00000 5.000000
25% 14,000000 6,50000 6,000000
50% 18.500000 8.00000 8.500000
75% 20.500000 9.00000 10.250000
max 28.000000 12.00000 12.000000

デフォルトでは、 describe()関数は各変数の 25、50、および 75 パーセンタイルを計算することに注意してください。

例 2: カスタム パーセンタイルでの description() の使用

次のコードは、 describe()関数をpercentiles引数とともに使用して、DataFrame 内の各数値変数の 30、60、および 90 パーセンタイルを計算する方法を示しています。

 #calculate custom percentiles for each numeric variable
df. describe (percentiles=[ .3 , .6 , .9 ])

           points assists rebounds
count 8.000000 8.00000 8.000000
mean 18.250000 7.75000 8.375000
std 5.365232 2.54951 2.559994
min 11.000000 4.00000 5.000000
30% 14.400000 7.00000 6.200000
50% 18.500000 8.00000 8.500000
60% 19.200000 9.00000 9.200000
90% 23.800000 9.90000 11.300000
max 28.000000 12.00000 12.000000

description()関数は、各数値変数の 30、60、および 90 パーセンタイルを返すことに注意してください。

: description()関数は、50 パーセンタイルも返します。これは、各変数の中央値を表し、 describe()関数によって計算されるデフォルトのメトリクスの 1 つであるためです。

例 3: パーセンタイルを使用せずに description() を使用する

次のコードは、 percentiles=[]引数を指定したdescription()関数を使用して、DataFrame 内の各数値変数のパーセンタイルを計算しない方法を示しています。

 #calculate no percentiles for each numeric variable
df. describe (percentiles=[])

           points assists rebounds
count 8.000000 8.00000 8.000000
mean 18.250000 7.75000 8.375000
std 5.365232 2.54951 2.559994
min 11.000000 4.00000 5.000000
50% 18.500000 8.00000 8.500000
max 28.000000 12.00000 12.000000

25 パーセンタイルと 75 パーセンタイルは各変数に対して計算されなくなったことに注意してください。

50 パーセンタイルは各変数の中央値を表すため、常に結果に含まれることに注意してください。

追加リソース

次のチュートリアルでは、パンダで他の一般的な操作を実行する方法を説明します。

パンダ: グループごとに description() を使用する方法
パンダ:describe() を使用して科学表記を削除する方法
パンダ: 平均、中央値、最頻値を計算する方法

コメントを追加する

メールアドレスが公開されることはありません。 が付いている欄は必須項目です