I am new to python. I want to find the top 2 largest values from all the columns for repetitive row elements (i.e. 5 to 4100), and also show its respective row and column index label in output.The largest value should be absolute. (Irrespective of + or - sign) My data structure is like image shown:
df = pd.DataFrame({'E_at_0': [43, -53, 45, -17, 45, 19, 11, 32, 36, 32],
'E_at_10': [-47, 47, 46, -18, 53, 16, 12, 34, -71, -34],
'E_at_20': [56, 43, 41, 29, 36, 14, 13, -37, 43, 38],
'E_at_30': [-46, 16, -40, 31, 42, 15, 63, -39, 52, 39]}, index=[5, 10, 12, 101, 4100, 5, 10, 12, 101, 4100])
df.index.name='Ele_Num'
Ele_num column is my index column.
The row indexs will keep on repeating for 'n'number of times. For every 'nth' data set, I want 2 largest values and their index positions.
code is using pandas
df = pd.read_csv ('trial.csv')
df = df.set_index('Ele_Num')
s = df.abs().stack()
mask = s == s.max()
df1 = df.stack()[mask].reset_index()
df1.columns = ['Element No','Column','Values']
print (df1)
df1.to_csv('trial_output.csv', encoding='utf-8', index=True)
Expected Results:
For 1st set of rows (i.e. for index 5,10,12,101,4100)
Ele_Num E_at_20
5 56
For 2nd set of rows (i.e.again for index 5,10,12,101,4100)
Ele_Num E_at_10
101 -71
The row indexs will keep on repeating for 'n'number of times. For every 'nth' data set, I want 2 largest values and their index positions.
Actual Results:
Ele_Num E_at_30
101 -71