pandas.DataFrame.mode#

DataFrame.mode(axis=0, numeric_only=False, dropna=True)[源代码]#

获取沿所选轴的每个元素的模式。

一组值的模式是最常出现的值。它可以是多个值。

参数
axis{0或‘index’,1或‘Columns’},默认为0

搜索模式时要迭代的轴:

  • 0或‘index’:获取每列的模式

  • 1或‘Columns’:获取每行的模式。

numeric_only布尔值,默认为False

如果为True,则仅适用于数字列。

dropna布尔值,默认为True

不考虑NAN/NAT的计数。

退货
DataFrame

每列或每行的模式。

参见

Series.mode

返回序列中的最高频率值。

Series.value_counts

返回数列中的值的计数。

示例

>>> df = pd.DataFrame([('bird', 2, 2),
...                    ('mammal', 4, np.nan),
...                    ('arthropod', 8, 0),
...                    ('bird', 2, np.nan)],
...                   index=('falcon', 'horse', 'spider', 'ostrich'),
...                   columns=('species', 'legs', 'wings'))
>>> df
           species  legs  wings
falcon        bird     2    2.0
horse       mammal     4    NaN
spider   arthropod     8    0.0
ostrich       bird     2    NaN

缺省情况下,不考虑缺少的值,并且WINDS的模式都是0和2。 specieslegs 包含 NaN

>>> df.mode()
  species  legs  wings
0    bird   2.0    0.0
1     NaN   NaN    2.0

设置 dropna=False NaN 考虑了值,它们可以是模式(就像机翼一样)。

>>> df.mode(dropna=False)
  species  legs  wings
0    bird     2    NaN

设置 numeric_only=True ,则只计算数值列的模式,而忽略其他类型的列。

>>> df.mode(numeric_only=True)
   legs  wings
0   2.0    0.0
1   NaN    2.0

要计算列而不是行的模式,请使用AXIS参数:

>>> df.mode(axis='columns', numeric_only=True)
           0    1
falcon   2.0  NaN
horse    4.0  NaN
spider   0.0  8.0
ostrich  2.0  NaN