I have a pandas DataFrame containing probabilities. What I need is a new DataFrame where each row contains the indices of the sorted row. Example:
df = pd.DataFrame([[0.5,0.7,0.1],[0.1,0.7,0.5]])
df
0 1 2
0 0.5 0.7 0.1
1 0.1 0.7 0.5
new_df = some sort operation on df
new_df
0 1 2
0 1 0 2
1 2 0 1
I know how to do it the hard way, using multiple loops and such. I found references to sorting values but no "sorting" function that returned the indices. Anyone some suggestion?
.indexto get the indices for each column after sort (ensure NOT inplace), then build the required dataframe!df(the output suggestsdf[1][1]was0.5instead of the0.7stated in the example). I'd probably just dodf.transform(lambda s: np.argsort(-s.values), axis=1)