其他分享
首页 > 其他分享> > pandas众数mode()

pandas众数mode()

作者:互联网

官方文档里的例子
Examples -------- >>> 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 By default, missing values are not considered, and the mode of wings are both 0 and 2. The second row of species and legs contains ``NaN``, because they have only one mode, but the DataFrame has two rows.
不负责任的翻译:默认不考虑缺失值,wings的众数 0 和 2。第二行中species和legs含有“NaN”,
因为它们都仅有一个众数,但DataFrame 有两行,所以凑数补个NaN。 
>>> df.mode()
  species  legs  wings
0    bird   2.0    0.0
1     NaN   NaN    2.0

Setting ``dropna=False`` ``NaN`` values are considered and they can be
the mode (like for wings).
不负责任的翻译:设置dropna='False',即考虑计算缺失值Nan的数量
>>> df.mode(dropna=False)
  species  legs  wings
0    bird     2    NaN

Setting ``numeric_only=True``, only the mode of numeric columns is
computed, and columns of other types are ignored.
不负责任的翻译:设置参数numeric_only=True,即仅统计数字的众数
>>> df.mode(numeric_only=True)
   legs  wings
0   2.0    0.0
1   NaN    2.0

To compute the mode over columns and not rows, use the axis parameter:
不负责任的翻译:通过设置axis轴参数,可以选择统计行或列
axis='columns'或axis='index'

发现axis=0或axis=1也可以

>>> 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

 

标签:legs,NaN,pandas,mode,众数,2.0,wings,axis
来源: https://www.cnblogs.com/xuwinwin/p/15758982.html