Find names of top-n highest-value columns in each pandas dataframe row
You could use np.argsort to find the indices of the n largest items 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, … Read more