0

So I have this Pandas DataFrame.

DataFrame

I am trying to figure out how to write this in python.

For each row I want to say, "if the 'Bid_Size' is a different value that the 'Bid_Size' in the previous row, highlight this cell.

For instance in my provided data set, row #3/column-'Bid_Size' would be highlighted because it is a different value than the 'Bid_Size' in the previous row.

I am guessing it would go something like

import pandas as pd
file = pd.read_csv('DataBase.csv', header=None, names=['Book_ID','Asset','Best_Bid','Bid_Size','Best_Ask', 'Ask_Size'])  

df = pd.DataFrame(file)

def highlight_yellow(df):
    for rows in df:
        if df['Best_Bid'] != -['Best_Bid']:
            return['highlight:yellow']

df.style.apply(highlight_yellow)

Thank you, I just can not figure this one out.

1
  • 1
    don't post your dataframe as an image. post it as text. do you expect us to transcribe it from the image? Commented Dec 30, 2020 at 0:07

2 Answers 2

2

You could shift Best_Bid down one and then compare to assign a value. First create a highlight column with your default value, then reassign.

>>> df=pd.DataFrame({"Best_Bid":[1,1,1,2,2,5,1,5,4,4,4]})
>>> df["Highlight"] = ""
>>> df["Highlight"][df["Best_Bid"] != df["Best_Bid"].shift(1)] = "highlight:yellow"
>>> df
    Best_Bid         Highlight
0          1  highlight:yellow
1          1                  
2          1                  
3          2  highlight:yellow
4          2                  
5          5  highlight:yellow
6          1  highlight:yellow
7          5  highlight:yellow
8          4  highlight:yellow
9          4                  
10         4                  
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your reply. I am trying to figure out now how to apply this to highlight cells within my 'Best_Bid column. This gave me some good framework to use!
0

I don't know what kind of data you have, but I will make an example. Suppose that you have this dataframe:

import pandas as pd

df = pd.DataFrame({
    'Best_Bid' : ['value_a', 'value_b', 'value_a','value_a']
})

Now create a function to paint your rows:

 def highlight(column_value, value_to_compare):
    color_a = 'background-color: yellow'
    color_b = 'background-color: red'
    return color_a if column_value==value_to_compare else color_b

Now, apply this function:

df.style.applymap(lambda x: highlight(x, "value_a"))

And you will get:

enter image description here

This function will allow you to handle your color and indexes in an independent way!

4 Comments

couple of things: I think the columns should display their original values. also, numpy.where would be useful in consolidating your two assignments
yes, I had an error in my solution, thanks!
Thank you very much for your response! I am having trouble as to how to apply your function to my dataset, but you have given me some starting ground!
Great to know that. If you find useful my answer, please vote for it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.