With a given pandas dataframe, I'd like to create new columns for the highest, second highest, third highest, etc... values in a row. And then create another column for the corresponding column name of each of those. The code below does this for the max value of the row, but not those that follow.
Adapted from Find the column name which has the maximum value for each row
import pandas as pd
df = pd.DataFrame({'A': (23, 24, 55, 77, 33, 66),
'B': (12, 33, 0.2, 44, 23.5, 66),
'C': (1, 33, 66, 44, 5, 62),
'D': (9, 343, 4, 64, 24, 63),
'E': (123, 33, 2.2, 42, 2, 99)})
# Determine the max value and column name and add as columns to df
df['Max1'] = df.max(axis=1)
df['Col_Max1'] = df.idxmax(axis=1)
# Determine the 2nd and 3rd max PR and threshold levels and add as columns
# ???????????
print(df)
This produces:
A B C D E Max1 Col_Max1
0 23 12.0 1 9 123.0 123.0 E
1 24 33.0 33 343 33.0 343.0 D
2 55 0.2 66 4 2.2 66.0 C
3 77 44.0 44 64 42.0 77.0 A
4 33 23.5 5 24 2.0 33.0 A
5 66 66.0 62 63 99.0 99.0 E
Process finished with exit code 0
Only caveat would be that it is possible to have a very large number of columns, if that matters for performance. Thanks guys.