Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncomfortable output of mode() in pandas Dataframe

I have a dataframe with several columns (the features).

>>> print(df)

   col1  col2
a     1     1
b     2     2
c     3     3
d     3     2

I would like to compute the mode of one of them. This is what happens:

>>> print(df['col1'].mode())

0    3
dtype: int64

I would like to output simply the value 3. This behavoiur is quite strange, if you consider that the following very similar code is working:

>>> print(df['col1'].mean())

2.25

So two questions: why does this happen? How can I obtain the pure mode value as it happens for the mean?

like image 939
Bernheart Avatar asked Mar 19 '17 10:03

Bernheart


People also ask

WHAT IS mode () in pandas?

Pandas DataFrame: mode() function The mode() function is used to 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.

What does Mode return in pandas?

The mode is the value that appears most often. There can be multiple modes. Always returns Series even if only one value is returned. Don't consider counts of NaN/NaT.

How do you check mode in pandas?

Finding the mode in a column, or the mode for all columns or rows in a DataFrame using pandas is easy. We can use the pandas mode() function to find the mode value of columns in a DataFrame. The pandas mode() function works for both numeric and object dtypes.


Video Answer


1 Answers

Because Series.mode() can return multiple values:

consider the following DF:

In [77]: df
Out[77]:
   col1  col2
a     1     1
b     2     2
c     3     3
d     3     2
e     2     3

In [78]: df['col1'].mode()
Out[78]:
0    2
1    3
dtype: int64

From docstring:

Empty if nothing occurs at least 2 times. Always returns Series even if only one value.

If you want to chose the first value:

In [83]: df['col1'].mode().iloc[0]
Out[83]: 2

In [84]: df['col1'].mode()[0]
Out[84]: 2
like image 67
MaxU - stop WAR against UA Avatar answered Oct 28 '22 05:10

MaxU - stop WAR against UA