little girl
little girl

Reputation: 295

find 3 largest values in every column in data frame and get the index number python

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

Upvotes: 2

Views: 2869

Answers (3)

Mayank Porwal
Mayank Porwal

Reputation: 34056

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

Upvotes: 1

timgeb
timgeb

Reputation: 78690

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

Upvotes: 1

jpp
jpp

Reputation: 164683

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

Upvotes: 1

Related Questions