「Python」pandas.Seriesの要素の最頻値を取得するサンプル
環境
pandas 1.4.2
PyCharm 2021.3.3
構文
1.class pandas.Series(data=None, index=None, dtype=None,
name=None, copy=False, fastpath=False)
パラメーター
dataarray-like、Iterable、dict、またはscalar value シリーズに保存されたデータが含まれます。 データがdictの場合、引数の順序は維持されます。 indexarray-likeまたはIndex(1d) 値はハッシュ可能で、データと同じ長さである必要があります。 dtypestr、numpy.dtype、またはExtensionDtype、オプション 出力シリーズのデータタイプ。 namestr、オプション シリーズに付ける名前。
2.Series.mode(dropna=True)
シリーズのモードを返します。
モードは、最も頻繁に表示される値です。 複数のモードがあります。
値が1つだけ返される場合でも、常にSeriesを返します。
パラメーター
dropnabool、デフォルトはTrue NaN/NaTの数を考慮しないでください。
戻り値 シリーズ
ソートされた順序でのシリーズのモード。 pandas.Seriesからmode()を呼ぶとpandas.Seriesが返ります
使用例
# coding: utf-8
import pandas as pd
cft = pd.Series(['S', 'S', 'T', 'R'])
print("結果1")
print(cft)
print("結果2")
print(cft.mode())
print("最頻値の結果")
modeVal = cft.mode()[0]
print(modeVal)
実行結果
結果1 0 S 1 S 2 T 3 R dtype: object 結果2 0 S dtype: object 最頻値の結果 S