Change unicode text emoji color using python with pandas style

Question:

Context: I’m trying to show this emoji as a triangle point up (like this one ), but in green.

Let’s say we’re trying to apply a formatting style to a table to display it as HTML as such:

arrays = [
    ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
    ["one", "two", "one", "two", "one", "two", "one", "two"],
]

tuples = list(zip(*arrays))

index = pd.MultiIndex.from_tuples(tuples, names=[None, "Index"])

df = pd.DataFrame(np.random.randn(-3, 8), index=["A", "B", "C"], columns=index)

df.style
   .format(
        lambda x :"  {:.0%}".format(x) if x > 0 else "  {:.0%}".format(x) if "{:.0%}".format(x) == '0%' else "  {:.0%}".format(x))

df.style.set_table_styles(styles).to_html()

How can I make it so that the triangle up is shown in HTML as green?

Essentially the desired output would be a triangle pointing up but green instead of red (I don’t know why this hasn’t been submitted yet to unicode to add it). Any ideias to how I can do this in python using pandas style?

Thanks!

I’ve seen this SO post: Color for Unicode Emoji

But not sure how we could achieve that with the style formatting in python.

I believe we could do this:

td_props = [
  ('font-size', '16px'),
  ('font-weight', 'bold'),
  ('text-align', 'center'),
  ('width','95vw'),
  ('white-space','nowrap'),
  ('border','1px solid #353148'),

]

styles = [
    dict(selector="td", props=td_props),
]

    df.style
       .format(
            lambda x :"  {:.0%}".format(x) if x > 0 else "  {:.0%}".format(x) if "{:.0%}".format(x) == '0%' else "  {:.0%}".format(x)
       .set_table_styles(styles))

But I’m not sure how we could inject css into the formatter

Asked By: user139442

||

Answers:

You can try something like this (it will change the font color as well, however if anyone knows how to change the color of just the emoji feel free to post as well):

df.style
  .format(lambda x :"  {:.0%}".format(x) if x > 0 else "  {:.0%}".format(x) if "{:.0%}".format(x) == '0%' else "  {:.0%}".format(x))
  .applymap(lambda v: 'color:transparent; text-shadow: 0 0 0 limegreen;;' if (v > 0 ) else None).to_html()

enter image description here

Answered By: Chronicles
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.