0

enter code here I have code, where I am able to generate the maximum value, but I need to highlight that maximum value (or at least cell address of that value).

I tried to apply the style, but it is throwing some error:

AttributeError: module 'numpy' has no attribute 'style'

My code would be like this :

import pandas as pd 
import numpy as np

dataset = pd.read_csv("ALL_CURVES.csv")
dataset['groups'] = dataset.index//192
Results=dataset.groupby('groups').max()
Results['groups'] = Results.index//20
outgrid=Results.iloc[:,1].values

#####
X=outgrid
x=np.array(X)
output=np.reshape(x, (9,-1 ))
max_output=output.max()
np.amax(output, axis=None, out=None)
np.style.apply(highlight_max, color='darkorange', axis=1)
#####
#print(output)

np.savetxt('output.csv', output,fmt="%2.2f", delimiter=',')

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

You can do something like this:

df = pd.DataFrame({'A':['15.45','78.7456','79.24', '75.45'],
                   'B':['78.6','45.23','45.4', '34.45'],
                   'C':['8.6','5.23','5.4', '4.45']})
print (df)

def highlight_max(data, color='red'):
    attr = 'background-color: {}'.format(color)
    data = data.astype(float)
    if data.ndim == 1:  
        is_max = data == data.max()
        return [attr if v else '' for v in is_max]
    else: 
        is_max = data == data.max().max()
        return pd.DataFrame(np.where(is_max, attr, ''),
                            index=data.index, columns=data.columns)

This highlights the maximum values in the dataframe
enter image description here
And with this you can save it to excel.

df  = df.style.apply(highlight_max)
print(df)
dfPercent.to_excel('file.xlsx')

A similar answer is here Python Pandas - Highlighting maximum value in column

Community
  • 1
  • 1
SSharma
  • 951
  • 6
  • 15