Home > Net >  How to display a styled panda dataframe over a for loop?
How to display a styled panda dataframe over a for loop?

Time:04-10

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?

CodePudding user response:

You can use display:

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

CodePudding user response:

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'
  • Related