Get first and second highest values in pandas columns

后端 未结 5 1663
春和景丽
春和景丽 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:50

    You could just sort your results, such that the first rows will contain the max. Then you can simply use indexing to get the first n places.

    RawResults = Results.ix[:, 'Unnamed: 9': 'Zeb'].sort_values(by='votes', ascending=False)
    RawResults.iloc[0, :] # First place
    RawResults.iloc[1, :] # Second place
    RawResults.iloc[n, :] # nth place
    

提交回复
热议问题