Viewing Pandas DataFrame in VS Code

In this post I will show you how to access the Data viewer which is a useful tool to review, sort and filter data within a Pandas DataFrame.

Required VS Code Extension

If you have not already done so, add the Python extension for Visual Studio Code from Microsoft.

Code Sample

In the code example below, the English Premier League football is read from a HTML table into a list of DataFrames.

import pandas as pd

url = 'https://www.msn.com/en-au/sport/football/epl/ladder'

df_list = pd.read_html(url)

print(df_list[0])

The last line prints out the contents of the Dataframe. Pandas read_html returns a list so the print function is called and outputs the first and only entry in the list which is found at index 0.

Output via print()

Running the output using using print() gives a satisfactory output that is good enough for a first cut to review the data.

Data Viewer

It is also possible to view Pandas Datagrid with the more interactive Data Viewer. This feature is only available when running a program via the VS Code Debugger.

Step 1

Add a break point to the program by clicking in the gutter. In the example shown below I have added a breakpoint at line 7

Step 2

Start the debugger by clicking the debug icon on the side bar and then select Run and Debug

Step 3

Once the program hits the breakpoint, expand the df_list variable and right click where the red border is marked and then select View Value in Data Viewer

Step 4

With the Data Viewer visible, you can interact with the contents of the DataFrame with sort and filter operations.

Very nice! But is there an easier way?

Whilst the Data viewer is very useful, getting to it by having to run the debugger can be tiresome. If you are aware of a simpler or more elegant method of displaying the Data Viewer please let me know via the comments or by getting in touch.

4 thoughts on “Viewing Pandas DataFrame in VS Code

  1. Hi! In response to the call to leave a comment if there’s a more elegant way –

    1. I work with files as Jupyter notebooks (ipnyb) instead of the .py extension.
    2. Instead of typing print(df), I just type df on the next line and the output is very pretty.

    Doesn’t allow for filtering like debug mode does, but I don’t see myself filtering all too much anyways, just getting a quick view. Eager to know what you think!

  2. Perfect! I like it, I wouldn’t have thought of that.

    I appreciate you taking the time to reply. Thank you.

    1. Thanks for getting in touch, unfortunately I haven’t seen this problem before. I appreciate you have already tried this but many times when VS Code does strange things a restart of VS Code has normally fixed the issue.

      If you do solve it please let me know.

      Regards

      Ian

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.