How to print dataframe in VS Code notebooks without print (or tabulate)?

Viewed 47

Is it possible for VS Code notebooks to display my pandas dataframe the same way it does alone as when running in a loop?

If I call df at the end of a cell, I get a beautiful output

df

Beautiful output of dataframe

I'm making many dataframes in a loop and want to inspect them. But the output doesn't look very nice.

for i in range(N):
    df = make_df(N)
    print(df)

Ugly output

Eww. It even goes over 2 lines :(

I have read through this question - Pretty print a pandas dataframe in VS Code - and the tabulate package is helpful but it isn't the same as native VS Code output.

1 Answers

Adding an answer here as it helped the OP:

You can use:

display(df)

From the documentation:

Display a Python object in all frontends.

By default all representations will be computed and sent to the frontends. Frontends can decide which representation is used and how.

In terminal IPython this will be similar to using print(), for use in richer frontends see Jupyter notebook examples with rich display logic.

https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html

Related