Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is_max = s == s.max() | How should I read this?

While studying Pandas Style, I got to the following:

def highlight_max(s):
    '''
    highlight the maximum in a Series yellow.
    '''
    is_max = s == s.max()
    return ['background-color: yellow' if v else '' for v in is_max]

How should I read is_max = s == s.max()?

like image 954
KcFnMi Avatar asked Oct 12 '16 11:10

KcFnMi


3 Answers

s == s.max() will evaluate to a boolean (due to the == in between the variables). The next step is storing that value in is_max.

like image 163
Mathias711 Avatar answered Oct 25 '22 16:10

Mathias711


In pandas s is very often Series (column in DataFrame).

So you compare all values in Series with max value of Series and get boolean mask. Output is in is_max. And then set style 'background-color: yellow' only to cell of table where is True value - where is max value.

Sample:

s = pd.Series([1,2,3])
print (s)
0    1
1    2
2    3
dtype: int64

is_max = s == s.max()
print (is_max)
0    False
1    False
2     True
dtype: bool
like image 34
jezrael Avatar answered Oct 25 '22 16:10

jezrael


The code

is_max = s == s.max()

is evaluated as

is_max = (s == s.max())

The bit in parentheses is evaluated first, and that is either True or False. The result is assigned to is_max.

like image 31
Bathsheba Avatar answered Oct 25 '22 16:10

Bathsheba