How to display a styled panda dataframe over a for loop?

Question:

I’m trying to display a dataframe with a specific style twice (output twice on jupyter notebook). It doesn’t show up when I use a for loop.

## having already created dataframe df

for i in range(2):
      df.style.set_table_styles([{'selector' : '','props' : [('border',
                                        '10px solid yellow')]}])

However it works when I do this:

df.style.set_table_styles([{'selector' : '','props' : [('border',
                                        '10px solid yellow')]}])

df.style.set_table_styles([{'selector' : '','props' : [('border',
                                        '10px solid yellow')]}])

How can I get it to work in a for loop?

Asked By: amajester

||

Answers:

You can use display:

for i in range(2):
  display(df.style.set_table_styles([{'selector' : '','props' : [('border',
                                    '10px solid yellow')]}]))
Answered By: notiv

By default, Jupyter cells only display the last expression output.

To change this behavior and display all expressions in a cell when you run it, you can add this line at the beginning of your notebook:

from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"  # default='last_expr'
Answered By: Laurent