1

I am working on a jupyter notebook right now and I am looking for a way to conditionally color each cell in a pandas dataframe according to its relative value within the column (or alternatively row).

The final output should be a pandas dataframe.
Conceptually it would be like creating a heatmap where the shading is defined independently for each column and is based on the max and min of the column itself.

I have had a look at this and this but in both they create actual plot as output instead of coloring the dataframe cells.

4
  • 1
    Do you mean coloring the output in the console? Commented Nov 21, 2019 at 12:42
  • right, I am sorry. I am working on a jupyter notebook right now =) I'll update the question Commented Nov 21, 2019 at 12:42
  • 1
    How do you display your dataframe? Have you looked at this: pandas.pydata.org/pandas-docs/stable/user_guide/style.html ? Commented Nov 21, 2019 at 12:43
  • Thanks @KarlAnka, didn't know about it Commented Nov 21, 2019 at 12:51

2 Answers 2

6

You can find more options here: Pandas DataFrame Styling: Builtin Styles

import seaborn as sns
cm = sns.light_palette("green", as_cmap=True)

df.style.background_gradient(cmap=cm, axis=0) # explicitly applying column-wise

The output will look like:
enter image description here

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

Comments

2

You can use the style method. The output looks like a dataframe, but is not.
If you want each column to have a color gradient corresponding to the values of the column:

df.style.background_gradient()

To apply the style row-wise, use the additional parameter axis=1.

Comments

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.