Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.mode#

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

Get the mode(s) of each element along the selected axis.

The mode of a set of values is the value that appears most often.It can be multiple values.

Parameters:
axis{0 or ‘index’, 1 or ‘columns’}, default 0

The axis to iterate over while searching for the mode:

  • 0 or ‘index’ : get mode of each column

  • 1 or ‘columns’ : get mode of each row.

numeric_onlybool, default False

If True, only apply to numeric columns.

dropnabool, default True

Don’t consider counts of NaN/NaT.

Returns:
DataFrame

The modes of each column or row.

See also

Series.mode

Return the highest frequency value in a Series.

Series.value_counts

Return the counts of values in a Series.

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  wingsfalcon        bird     2    2.0horse       mammal     4    NaNspider   arthropod     8    0.0ostrich       bird     2    NaN

By default, missing values are not considered, and the mode of wingsare both 0 and 2. Because the resulting DataFrame has two rows,the second row ofspecies andlegs containsNaN.

>>>df.mode()  species  legs  wings0    bird   2.0    0.01     NaN   NaN    2.0

Settingdropna=FalseNaN values are considered and they can bethe mode (like for wings).

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

Settingnumeric_only=True, only the mode of numeric columns iscomputed, and columns of other types are ignored.

>>>df.mode(numeric_only=True)   legs  wings0   2.0    0.01   NaN    2.0

To compute the mode over columns and not rows, use the axis parameter:

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

[8]ページ先頭

©2009-2025 Movatter.jp