python pandas 3 smallest & 3 largest values

前端 未结 5 1011
误落风尘
误落风尘 2021-01-20 03:03

How can I find the index of the 3 smallest and 3 largest values in a column in my pandas dataframe? I saw ways to find max and min, but none to get the 3.

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-20 03:20

    You want to take a look at argsort (in numpy and in pandas)

    df = pd.DataFrame(np.random.randint(1,100,100).reshape(10,10))
    # bottom three indexes
    df[0].argsort().values[:3]    
    # top three indexes
    df[0].argsort().values[-3:]
    

提交回复
热议问题