2

I have data frame like this

        A         B         C         D    
0  0.037949  0.021150  0.127416  0.040137  
1  0.025174  0.007935  0.011774  0.003491  
2  0.022339  0.019022  0.024849  0.018062  
3  0.017205  0.051902  0.033246  0.018605  
4  0.044075  0.044006  0.065896  0.021264

And I want to get the data frame with the index values of 3 largest values in each columns. Desired output

       A         B         C         D    
0      4         3         0         0
1      0         4         4         4
2      1         0         3         3
jpp
  • 159,742
  • 34
  • 281
  • 339
little girl
  • 285
  • 1
  • 3
  • 15

3 Answers3

1

You can argsort via NumPy, then slice:

res = pd.DataFrame(df.values.argsort(0), columns=df.columns)\
        .iloc[len(df.index): -4: -1]

print(res)

   A  B  C  D
4  4  3  0  0
3  0  4  4  4
2  1  0  3  3
jpp
  • 159,742
  • 34
  • 281
  • 339
1

Something like this should work:

You can use nlargest function to get Top 3 values.

In [1979]: result = pd.DataFrame([df[i].nlargest(3).index.tolist() for i in df.columns]).T

In [1974]: result
Out[1974]: 
   A  B  C  D
0  4  3  0  0
1  0  4  4  4
2  1  0  3  3
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

Given

>>> df
          A         B         C         D
0  0.037949  0.021150  0.127416  0.040137
1  0.025174  0.007935  0.011774  0.003491
2  0.022339  0.019022  0.024849  0.018062
3  0.017205  0.051902  0.033246  0.018605
4  0.044075  0.044006  0.065896  0.021264

you can use DataFrame.apply in combination with Series.nlargest:

>>> df.apply(lambda s: pd.Series(s.nlargest(3).index))
   A  B  C  D
0  4  3  0  0
1  0  4  4  4
2  1  0  3  3
timgeb
  • 76,762
  • 20
  • 123
  • 145