Reputation: 273
I would like to get the cell that has max value, for example
A B C
A 1 5 5
B 2 6 3
C 0.5 1 2
Output:
Max 5 -> (A,B), (A,C)
Max 6 -> (B)
Max 2 -> (C)
i have tried this
col_new = df.columns.values
row_new = df.index.values
for row, cols in df.iterrows():
for col in range(len(df)):
if col_new == df.max():
print("Max (", row_new[row], ',', col_new[col], ')')
but the output is nothing
Upvotes: 0
Views: 47
Reputation: 13255
Use:
for row, cols in df.iterrows():
max_value = cols.max()
cols_list = cols[cols==max_value].index.tolist()
if len(cols_list)>1:
row_list = [row]*len(cols_list)
tup = tuple(zip(row_list,cols_list))
print(f'Max {max_value} -> {tup}')
else:
print(f'Max {max_value} -> {row,}')
Max 5.0 -> (('A', 'B'), ('A', 'C'))
Max 6.0 -> ('B',)
Max 2.0 -> ('C',)
We are multiplying the list with number of max columns as:
print(['A']*3)
['A', 'A', 'A']
Upvotes: 1