我使用以下命令来获取max元素:
print("%.2f" % total_returns['return 5 days ago'].max(), total_returns['return 5 days ago'].idxmax(), "5 days max")有没有办法也能得到第二,第三等最大的?
total_returns.head()
Out[15]:
first close last close ... date 1250 days ago date 1500 days ago
S&P 500 1469.25 2893.06 ... 2014-10-20 2013-10-22
Tesla 23.89 327.71 ... 2014-11-07 2013-11-11
Apple 3.67188 249.05 ... 2014-11-07 2013-11-11
Microsoft 58.375 144.19 ... 2014-11-07 2013-11-11
Amazon 76.125 1777.08 ... 2014-11-07 2013-11-11
[5 rows x 47 columns]发布于 2019-10-29 16:07:07
我猜您正在使用pandas.DataFrame。
在这种情况下,您可以使用pandas.DataFrame.nlargest
n=3
total_returns.nlargest(n, 'return 5 days ago')发布于 2019-10-29 16:53:37
对ascending=False使用DataFrame.sort_values,然后通过索引选择具有索引的第2位和第3位最大值:
print (total_returns[['first close']])
first close
S&P 500 1469.25000
Tesla 23.89000
Apple 3.67188
Microsoft 58.37500
Amazon 76.12500
s = total_returns['first close'].sort_values(ascending=False)
print (s)
S&P 500 1469.25000
Amazon 76.12500
Microsoft 58.37500
Tesla 23.89000
Apple 3.67188
Name: first close, dtype: float64
print("%.2f" % s.iloc[1],
s.index[1], "2nd largest")
76.12 Amazon 2nd largest
print("%.2f" % s.iloc[2],
s.index[2], "3rd largest")
58.38 Microsoft 3rd largesthttps://stackoverflow.com/questions/58603148
复制相似问题