Can’t get the pandas style.hide() method to work properly

  Kiến thức lập trình

I want to be able to hide certain rows/columns from my styled Dataframe post-styling.

This is how my dataframe/style object currently looks, and is produced by the below code

data = {
    'site': ['Brighton', 'London', 'Manchester', 'Leicester'],
    'wait_time': [10, 15, 20, 25],
    'wait_time_change_percent': [5, 10, -5, 0],
    'calls': [100, 150, 120, 130],
    'calls_change_percent': [10, -5, 20, 15],
    'complaints': [5, 10, 15, 20],
    'complaints_change_percent': [5, 10, -5, 0]
}

df = pd.DataFrame(data).set_index('site')

def color_change(val):
    color = 'red' if val < 0 else 'green'
    return f'color: {color}'

df_styled = df.style.applymap(lambda val: color_change(val), subset=pd.IndexSlice[:, ['wait_time_change_percent', 'calls_change_percent', 'complaints_change_percent']])

I’ve made this a lot simpler than my actual code just for example purposes so pls don’t ask me to use a different method as there’s a reason I have to remove the columns after the styling and not before 🙂

Now I thought I could use hide() by inserting this line:

df_styled_hidden = df_styled.hide(subset=['wait_time', 'calls', 'complaints'], axis=1)

But when I go to write df_styled_hidden, it just looks exactly the same as my screenshot. Is this a bug with style.hide() or am I just using it incorrectly? Thanks!

New contributor

Daniel Thomas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT