4.1. 平均値・中央値・最頻値#

データを代表する値を抽出するために、平均値や中央値、最頻値などが用いられます。

ここからは、次のcellでつくる pandasDataFrameに格納したテーブルデータを用いて平均値、中央値、最頻値を求めます。

import pandas as pd 
import numpy as np
df = pd.DataFrame({'price':[100, 400, 300, 1000 , 500, 1000, 400, 400, 240, 3000, 800, 2400],
                   'item_name':['pen', 'bread', 'brad', 'shirt', 'milk', 'T-shirt', 'apple', 
                           'pen', 'banana','T-shirt','bread', 'shirt'],
                   'number': [5, 4, 1, 0, 4, 200, 7, 4, 20, 100, 1, 4],
                   'created_at': pd.date_range('20180601', periods=12, freq= '627H') })
df.head(2)
price item_name number created_at
0 100 pen 5 2018-06-01 00:00:00
1 400 bread 4 2018-06-27 03:00:00
df.dtypes
price                  int64
item_name             object
number                 int64
created_at    datetime64[ns]
dtype: object

4.1.1. 平均値(mean)#

\[\frac{1}{N}\sum_ix_i\]

mean()int もしくは float の型をもつ Series へ適用できます。

dfの price 列の平均値を計算します。

df['price'].mean()
878.3333333333334
df['number'].mean()
29.166666666666668

int 型であるitem_nameには適用できないので次のcellはエラーになります。次のmedian()も同様に数値データ以外には適用できません。

df['item_name'].mean()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 df['item_name'].mean()

File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/series.py:6225, in Series.mean(self, axis, skipna, numeric_only, **kwargs)
   6217 @doc(make_doc("mean", ndim=1))
   6218 def mean(
   6219     self,
   (...)
   6223     **kwargs,
   6224 ):
-> 6225     return NDFrame.mean(self, axis, skipna, numeric_only, **kwargs)

File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/generic.py:11992, in NDFrame.mean(self, axis, skipna, numeric_only, **kwargs)
  11985 def mean(
  11986     self,
  11987     axis: Axis | None = 0,
   (...)
  11990     **kwargs,
  11991 ) -> Series | float:
> 11992     return self._stat_function(
  11993         "mean", nanops.nanmean, axis, skipna, numeric_only, **kwargs
  11994     )

File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/generic.py:11949, in NDFrame._stat_function(self, name, func, axis, skipna, numeric_only, **kwargs)
  11945 nv.validate_func(name, (), kwargs)
  11947 validate_bool_kwarg(skipna, "skipna", none_allowed=False)
> 11949 return self._reduce(
  11950     func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
  11951 )

File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/series.py:6133, in Series._reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)
   6128     # GH#47500 - change to TypeError to match other methods
   6129     raise TypeError(
   6130         f"Series.{name} does not allow {kwd_name}={numeric_only} "
   6131         "with non-numeric dtypes."
   6132     )
-> 6133 return op(delegate, skipna=skipna, **kwds)

File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/nanops.py:147, in bottleneck_switch.__call__.<locals>.f(values, axis, skipna, **kwds)
    145         result = alt(values, axis=axis, skipna=skipna, **kwds)
    146 else:
--> 147     result = alt(values, axis=axis, skipna=skipna, **kwds)
    149 return result

File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/nanops.py:404, in _datetimelike_compat.<locals>.new_func(values, axis, skipna, mask, **kwargs)
    401 if datetimelike and mask is None:
    402     mask = isna(values)
--> 404 result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs)
    406 if datetimelike:
    407     result = _wrap_results(result, orig_values.dtype, fill_value=iNaT)

File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/nanops.py:720, in nanmean(values, axis, skipna, mask)
    718 count = _get_counts(values.shape, mask, axis, dtype=dtype_count)
    719 the_sum = values.sum(axis, dtype=dtype_sum)
--> 720 the_sum = _ensure_numeric(the_sum)
    722 if axis is not None and getattr(the_sum, "ndim", False):
    723     count = cast(np.ndarray, count)

File /opt/anaconda3/lib/python3.11/site-packages/pandas/core/nanops.py:1693, in _ensure_numeric(x)
   1690 elif not (is_float(x) or is_integer(x) or is_complex(x)):
   1691     if isinstance(x, str):
   1692         # GH#44008, GH#36703 avoid casting e.g. strings to numeric
-> 1693         raise TypeError(f"Could not convert string '{x}' to numeric")
   1694     try:
   1695         x = float(x)

TypeError: Could not convert string 'penbreadbradshirtmilkT-shirtapplepenbananaT-shirtbreadshirt' to numeric

4.1.2. 中央値(median)#

データを大きさの順に並べたときに全体の中央にある値。つまり最大を取るデータと最小を取るデータのちょうど中間の位置にあるデータ。

median()を使ってdfの price 列の中央値を計算します。

df['price'].median()
450.0

4.1.3. 最頻値(mode)#

出現回数が最も多い値.

mode()dfの price 列の中央値を計算します。 ドキュメントも参照してください。

df['price'].mode()
0    400
Name: price, dtype: int64
type(df['price'].mode())
pandas.core.series.Series

最頻値の値のみを抽出したい場合は、上のDataFrameの値を抽出してください。

mode_value = df['price'].mode().iloc[0]

print(mode_value, '\n', type(mode_value))
400 
 <class 'numpy.int64'>

平均値(mean)、中央値(median)、最頻値(mode)の抽出は複数の列にも適用できます。

その場合は、データの型が int もしくは float の列の結果を返します。

multiple_means = df[['price','number']].mean()
print(multiple_means)
price     878.333333
number     29.166667
dtype: float64
multiple_medians = df[['price','number']].median()
print(multiple_medians)
price     450.0
number      4.0
dtype: float64
multiple_modes = df[['price','number']].mode()
display(multiple_modes)
price number
0 400 4

上のcellで作った multiple_modesのうち priceの最頻値のみを抽出したい場合は、DataFrame multiple_modes の要素を指定します。

num_mode = multiple_modes.loc[0,'number']
print(num_mode)
4