return n smallest indexes by column using pandas

后端 未结 3 1668
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 07:26

I have the following (simplified) dataframe:

df = pd.DataFrame({\'X\': [1, 2, 3, 4, 5,6,7,8,9,10],
\'Y\': [10,20,30,40,50,-10,-20,-30,-40,-50],
\'Z\': [20,18         


        
3条回答
  •  天命终不由人
    2020-12-31 08:13

    You can use apply with nsmallest:

    n = 3
    df.apply(lambda x: pd.Series(x.nsmallest(n).index))
    
    #   X   Y   Z
    #0  A   J   J
    #1  B   I   I
    #2  C   H   H
    

提交回复
热议问题