Uncomfortable output of mode() in pandas Dataframe

前端 未结 1 1369
情深已故
情深已故 2021-01-13 07:01

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
         


        
1条回答
  •  不思量自难忘°
    2021-01-13 07:28

    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
    

    0 讨论(0)
提交回复
热议问题