Find the top-level highest column values ​​in each pandas frame data row

I have the following framework:

  id     p1 p2 p3 p4
  1      0  9  1  4
  2      0  2  3  4
  3      1  3 10  7
  4      1  5  3  1
  5      2  3  7 10

I need to change the data frame so that for each identifier it has the top 3 columns with the highest values. The result will be like this:

 id top1 top2 top3
  1  p2   p4   p3
  2  p4   p3   p2
  3  p3   p4   p2
  4  p2   p3   p4/p1
  5  p4   p3   p2

It features the top three best sellers for each user_id. I already did this with a package dplyrin R, but I'm looking for the equivalent of pandas.

+4
source share
2 answers

You can use np.argsortto find the indices of the n largest elements for each row:

import numpy as np
import pandas as pd

df = pd.DataFrame({'id': [1, 2, 3, 4, 5],
 'p1': [0, 0, 1, 1, 2],
 'p2': [9, 2, 3, 5, 3],
 'p3': [1, 3, 10, 3, 7],
 'p4': [4, 4, 7, 1, 10]})
df = df.set_index('id')

nlargest = 3
order = np.argsort(-df.values, axis=1)[:, :nlargest]
result = pd.DataFrame(df.columns[order], 
                      columns=['top{}'.format(i) for i in range(1, nlargest+1)],
                      index=df.index)

print(result)

gives

   top1 top2 top3
id               
1    p2   p4   p3
2    p4   p3   p2
3    p3   p4   p2
4    p2   p3   p1
5    p4   p3   p2
+11
source

You can use:

df = df.set_index('id').apply(lambda x: pd.Series(x.sort_values(ascending=False)
       .iloc[:3].index, 
      index=['top1','top2','top3']), axis=1).reset_index()
print (df)
   id top1 top2 top3
0   1   p2   p4   p3
1   2   p4   p3   p2
2   3   p3   p4   p2
3   4   p2   p3   p4
4   5   p4   p3   p2
+5
source

Source: https://habr.com/ru/post/1651286/


All Articles