I have a dataframe with 3 kind of data types - int64, float64 and object (string). How to apply format for two different data types in one function?
Sample:
def abc_style_format(cell_val):
default = ""
style_a = "background-color: green"
style_b = "background-color: gold"
style_c = "background-color: salmon"
style_float = "precision=2"
match cell_val:
case "A":
return style_a
case "B":
return style_b
case "C":
return style_c
if type(cell_val) == "float64":
return style_float
return default
df_abc.style.map(abc_style_format)
Block match/case works properly, but part precision=2 doesn't work.
The result:

Here is one way to do it by defining as many helper functions as needed:
And one main function:
Then, with the following toy dataframe:
You can simply do:
Which gets you the expected output: