Get first and second highest values in pandas columns

后端 未结 5 1667
春和景丽
春和景丽 2020-12-15 05:34

I am using pandas to analyse some election results. I have a DF, Results, which has a row for each constituency and columns representing the votes for the various parties (o

5条回答
  •  感动是毒
    2020-12-15 05:52

    Here is an interesting approach. What if we replace the maximum value with the minimum value and calculate. Although it is a quick hack and, not recommended!

    first_highest_value_index = df.idxmax()
    second_highest_value_index = df.replace(df.max(),df(min)).idxmax()
    
    first_highest_value = df[first_highest_value_index]
    second_highest_value = df[second_highest_value_index]
    

提交回复
热议问题