Getting TypeError: reduction operation 'argmax' not allowed for this dtype when trying to use idxmax()

前端 未结 4 1552
[愿得一人]
[愿得一人] 2021-01-07 22:34

When using the idxmax() function in Pandas, I keep receiving this error.

Traceback (most recent call last):
  File \"/Users/username/College/yea         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-07 23:13

    The type of the cell values are, by default, non-numeric. argmin(), idxmin(), argmax() and other similar functions need the dtypes to be numeric.

    The easiest solution is to use pd.to_numeric() in order to convert your series (or columns) to numeric types. An example with a data frame df with a column 'a' would be:

    df['a'] = pd.to_numeric(df['a'])

    A more complete answer on type casting on pandas can be found here.

    Hope that helps :)

提交回复
热议问题