1

Is it possible to display each word with its color with python? We have an example of words with their colors:

    Mots             Poids      colors
 0  un              0.000007    #39e600
 1  bon             0.000005    #d9ffcc
 2  rapport         0.000009    #39e600
 3  qualité/prix.   0.000014    #269900

thank you in advance.

0

1 Answer 1

1

Using Pandas, it is possible to style a series for display in, for example, Jupyter notebook.

Here's an example:

df = pd.DataFrame({'Mots': ['un', 'bon', 'rapport', 'qualité/prix.'],
                   'colors': ['#39e600', '#d9ffcc', '#39e600', '#269900']})

def map_colors(x):
    df1 = x.copy()
    df1.loc[:, 'Mots'] = 'background-color: ' + df1.loc[:, 'colors']
    df1.loc[:, 'colors'] = 'background-color: '
    return df1

res = df.style.apply(map_colors, axis=None)

Result

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your rest. By the way, I'd like to make a horizontal display without putting it in a data frame. But I'll try based on your proposal to find a solution. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.