วิธีค้นหาค่าสูงสุดถัดไปในอาร์เรย์

ฉันต้องการค้นหาค่าสูงสุดถัดไปหลังจากได้รับค่า Max แรกในอาร์เรย์เดียวกันสำหรับสิ่งต่อไปนี้:

             Open    High     Low   Close    Volume
Date                                                
2017-07-03  370.24  371.35  351.50  352.62   6305401
2017-07-05  347.20  347.24  326.33  327.09  17046701
2017-07-06  317.26  320.79  306.30  308.83  19324495
2017-07-07  313.50  317.00  307.38  313.22  14176915
2017-07-10  312.90  317.94  303.13  316.05  13820889
2017-07-11  316.00  327.28  314.30  327.22  11559402
2017-07-12  330.40  333.10  324.50  329.52  10346127
2017-07-13  330.11  331.60  319.97  323.41   8594466
2017-07-14  323.19  328.42  321.22  327.78   5625211
2017-07-17  325.54  327.10  313.45  319.57   9876912
2017-07-18  317.50  329.13  315.66  328.24   6373720
2017-07-19  328.23  331.65  323.22  325.26   6357014
2017-07-20  326.90  330.22  324.20  329.92   5166188
2017-07-21  329.46  331.26  325.80  328.40   4901606

import datetime as dt
from datetime import timedelta as td
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
import numpy as np

start = dt.datetime(2017, 7, 1)
# retrieving data from google
df = web.DataReader('TSLA', 'google', start)
Dates = df.index
Highs = df['High'] # Getting only the values from the 'High' Column.
Highest_high = np.amax(Highs)  # returns the Highest value
> 371.35 #Output

Highests_index = Highs.argmax()  # returns the index of Highest value
> 2017-07-03 00:00:00 #Output
Highest_high_2 = ? # Highest High excluding the previous high discovered.
Highest_index_2 = ? # shall have index of the above Highest_high_2.

จะหา Highest_high_2 และ Highest_index_2 ได้อย่างไร?

ฉันใช้ argmax() ของ numpy เพื่อค้นหาดัชนีที่มีค่ามากที่สุดและ amax เพื่อค้นหาค่าที่ใหญ่ที่สุดเพียงอย่างเดียว

ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชม.


person BlueQuant    schedule 22.07.2017    source แหล่งที่มา
comment
ดูที่นี่: stackoverflow.com/questions/16225677/   -  person Arda Arslan    schedule 22.07.2017
comment
เยี่ยมมาก ขอบคุณสำหรับการอ้างอิง @ArdaArslan   -  person BlueQuant    schedule 22.07.2017


คำตอบ (2)


คุณสามารถทำได้ด้วยวิธีนี้

Highest_high_2 = df['High'].sort_values()[-2]

Highest_index_2 = df['High'].sort_values().index[-2]
person Community    schedule 28.07.2017

คุณสามารถเขียนโค้ดดังนี้:

a = np.array([10, 18, 20, 8, 15])
arg_max = a.argsort()[-2:][::-1]
highest_1 = a[arg_max[0]] #20
highest_2 = a[arg_max[1]] #18
person moh    schedule 22.07.2017
comment
ขอบคุณ แต่ฉันพบวิธีที่ง่ายกว่าและสะอาดกว่าในการใช้ sorted Highest_high_2 = sorted(Highs)[-2] - person BlueQuant; 23.07.2017