3 回答

TA貢獻1906條經(jīng)驗 獲得超10個贊
shift在這種情況下,數(shù)據(jù)幀的方法可以幫助您解決問題。
# Initialize dataframe
import pandas as pd
df = pd.DataFrame({
'CountryName': ['US', 'UK', 'Australia', 'Germany', 'France'],
'GDP': [350, 370, 340, 500, 450],
})
df = df.set_index('CountryName')
# Get the index value of the row directly before the row with max 'GDP' value
target = df['GDP'].shift(-1).idxmax()
結(jié)果如下:
In [1]: target
Out[1]: 'Australia'

TA貢獻1816條經(jīng)驗 獲得超4個贊

TA貢獻1772條經(jīng)驗 獲得超6個贊
這行得通嗎?
df = pd.DataFrame({'CountryName': ['US', 'UK', 'Australia', 'Germany', 'France'], 'GDP': [350, 370, 340, 500, 450]})
print(df['CountryName'][df['GDP'].idxmax()-1])
# Australia
我可以看到的一個問題是,最高的GDP索引0是否會返回數(shù)據(jù)框中的CountryName位置-1或最后一個國家/地區(qū)。
添加回答
舉報