Michael
Michael

Reputation: 5335

jupyter-notebook: print fine table in a cycle

When I input a name of dataframe in jupyter-notebook, it prints a pretty table:

fail_data

enter image description here

It can even recognize the TeX notation.

But when I need to print the data in the cycle, the output looks not so good:

fail_data_gr = fail_data.groupby('test_name')
for k, v in fail_data_gr:
    print(v)

enter image description here

How to make this output looking like the first one?

Upvotes: 2

Views: 3166

Answers (1)

cs95
cs95

Reputation: 402603

Displaying in jupyter notebooks delegate to the IPython.display module. When printing inside a loop, print dumps the __str__ representation of the DataFrame, which does no special rendering.

So, in summary, change

for k, v in fail_data_gr:
    print(v)

to

from IPython.display import display
for k, v in fail_data_gr:
    display(v)

Upvotes: 2

Related Questions