Is it possible to color code DataFrame cell values based on its value compared to the value returned from another function.
Here is an example of the DataFrame coloring that I created using the functions below. Dateframe Coloring](https://i.sstatic.net/iVhJeJdj.png)
For example:
I have a function that takes one argument, a contract value, and returns a tuple.
def net_60_comp(amount,f_rate=0.043):
dis_to_60 = amount * ((1+f_rate)**(60/365)-1)
dis_perc_to_60 = dis_to_60/amount
return(dis_to_60,dis_perc_to_60)
Could the value, "dis_to_60", returned from my above function be used in the color function below to determine color coding rather than using a hardcoded value?
def color (val):
v = net_60_comp
color = 'green' if int(val) > 80 else 'red'
return f'background-color: {color}'
df.style.applymap(color)
You can create
def color(val, other_value):
and use lambda
to run it with other_value
dis_to_60, dis_perc_to_60 = net_60_comp(...)
.applymap(lambda x:color(x, dis_to_60))
Documentation suggests that you can also use
.applymap(color, other_value=dis_to_60)