I have a Pandas dataframe that contains data of prices of various products as taken on different dates, the columns are ‘date’, ‘product’, ‘price’.
My goal is to highlight the price cell where there has been a price reduction for that particular product. Much like this example .csv seen below…
An example .csv showing what I want to achieve using Pandas Styling
I understand that each product will need to be separated and then the prices of that product evaluated in pairs. I have used the following code in another part of the script to successfully achieve this:
integer = 0
for iteration in range(iterations):
first_price_pair = one_product.iloc[integer,2]
integer=integer+1
second_price_pair = one_product.iloc[integer,2]
# one_product is selected by using .drop_duplicates() on 'product'
price_dif = first_price_pair - second_price_pair
if second_price_pair < first_price_pair:
# highlight cell green - INDICATES PRICE REDUCTION FROM PREV PRICE
elif second_price_pair == first_price_pair:
# no change to cell colour
elif second_price_pair > first_price_pair:
# highlight cell RED - INDICATES PRICE INCREASE FROM PREV PRICE
My problem is when I attempt to use - DataFrame.style - for applying the highlighting. It appears that once ‘styling’ has been applied to the DF, the DF is then converted to type: pandas.io.formats.style.Styler - and that this can then not be modified.
I’d appreciate it if someone can confirm it is possible to achieve what I’m trying to do and if so, give me some guidance on how to achieve it.
Thank you!