簡體   English   中英

Python:找到一個系列的最大值(從 pandas 數據幀產生)

[英]Python: find the maximum of a series (produced from a pandas dataframe)

我不知道為什么我找不到該系列的最大值。 有人可以幫忙嗎? 它由 pandas dataframe 生產

temp
Out[190]: 
0               0
1       0.0197017
2       0.0183603
3       0.0483337
4       0.0136385

8187    0.0800631
8188    0.0433753
8189    0.0673788
8190    0.0511651
8191    0.0811448
Name: adjr2, Length: 8192, dtype: object

type(temp)
Out[191]: pandas.core.series.Series

temp.idxmax(axis = 1 , skipna=True)

這給出了一條錯誤消息:

Traceback (most recent call last):

  File "<ipython-input-192-21f0a425e80d>", line 1, in <module>
    temp.idxmax(axis = 1 , skipna=True)

  File "/Users/miao/opt/anaconda3/lib/python3.7/site-packages/pandas/core/series.py", line 2277, in idxmax
    i = nanops.nanargmax(com.values_from_object(self), skipna=skipna)

  File "/Users/miao/opt/anaconda3/lib/python3.7/site-packages/pandas/core/nanops.py", line 67, in _f
    raise TypeError(msg.format(name=f.__name__.replace("nan", "")))

TypeError: reduction operation 'argmax' not allowed for this dtype

通過Series.astypeSeries轉換為數字:

temp.astype(float).idxmax(axis = 1 , skipna=True)

或者,如果某些非數值使用帶有errors='coerce'to_numeric將它們轉換為缺失值NaN

pd.to_numeric(temp, errors='coerce').idxmax(axis = 1 , skipna=True)

pandas(例如 max)中的許多聚合 function 不支持對非數字類型(如stringobject )的操作。 您可以嘗試將數據轉換為數字類型(這也將提高大數據集的性能)。

您可以使用temp.astype轉換系列。

In [47]: s
Out[47]:
a    1
b    2
c    3
Name: 0, dtype: object

In [48]: s.astype(float)
Out[48]:
a    1
b    2
c    3
Name: 0, dtype: float64

In [50]: s.astype(float).idxmax()
Out[50]: 'c'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM