-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Feature request: add an optional values parameter to Styler.background_gradient #22727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you provide an example? |
if I understand correctly what your trying to achieve, you could render the HTML directly yourself, using something like this?.. import pandas as pd
from IPython.display import HTML, display
from . import datasets
def make_clickable(val):
return '<a target="_blank" href="https://www.amazon.co.uk/dp/{}">{}</a>'.format(val, val)
def image_html(row):
if type(row['SmallImage_URL']) != float:
return '<img src="{}" style="height: {}px; width: {}px;">' \
.format(row['SmallImage_URL'], int(row['SmallImage_Height']), int(row['SmallImage_Width']))
else:
return ''
def add_image_to_dataframe(df):
df = df.copy()
columns = df.columns.tolist()
image_metadata_columns = ['SmallImage_Height',
'SmallImage_Width', 'SmallImage_URL']
df = datasets.add_item_attributes(df, image_metadata_columns)
df['image'] = df.apply(image_html, axis=1)
df = df.drop(columns=image_metadata_columns)
return df[['image'] + columns]
def render(df, add_image=True):
if not df.empty is True:
df = df.copy().fillna("")
if 'asin' not in df.columns:
df = datasets.add_asin(df)
if (add_image):
df = add_image_to_dataframe(df)
df['asin'] = df.asin.apply(make_clickable)
with pd.option_context('display.max_colwidth', -1):
html = df.to_html(index=False, escape=False)
display(HTML(html)) |
For example, I want to have background gradient colors for 2 columns Ctrl and Test in a DataFrame, but the background gradient colors are decided according to the difference of the 2 columns. I achieved it as below.
|
It's not clear to me what the API would look like. IIRC, there's nothing else in Styler where a property for a column is computed by the values in another array. It is unfortunate that you needed to duplicate the code in |
I think this is fairly easy to deal with with an introduction of a new argument.
I'll get around to this at some point.. |
Sometimes we want to apply background_gradient to some columns according to values in another column. There's no easy way to do this currently. It can be made possible by adding an optional values parameter. And when the values parameter is specified, it is used to generate background colors.
The text was updated successfully, but these errors were encountered: