[Python] View dataframe in VScode debug console

Task

Premise

I like VScode

Solution overview (table of contents)

In VScode

  1. Set a breakpoint
  2. Run in debug mode
  3. Use the debug console
  4. Add a line to launch.json to display the data frame

Solution details

Set a breakpoint

Move the mouse cursor to the left of the line number and click to add a red circle image.png

Run in debug mode

  1. Click the bug-like mark to go to the debug screen
  2. Click the run button for debugging image.png

Use debug console

Go to the debug console screen, enter the variable name and press the enter key to check various things image.png I'm having trouble displaying the data frame properly </ font>

Added a line to launch.json to display the data frame

Press cmd + shift + P to open the command palette Type "launch" and Click Open launch.json image.png

Add "" redirectOutput ": true" and save by overwriting (don't forget "," at the end of the previous line) image.png

Finish debugging once and start debugging again. Try print (df) on the debug console image.png Data frames are arranged and arranged for easy viewing </ font>

  • I don't know why this works. Gugu arrived at ↓ and imitated it. https://github.com/microsoft/ptvsd/issues/2036#issuecomment-573343490

Bonus) Multi-line processing in the debug console

You can also write multi-line processing with sht + Enter image.png

↓ Execution result image.png

Supplement

Operating environment: Mac, Python3.7

Sample code to be debugged:

# create test data
a = 15

import pandas as pd
df = pd.DataFrame(
    columns = ['name', 'gender', 'age'], 
    data = [
        ['john', 'man', 25],
        ['yoko', 'woman', 28],
        ['kevin', 'man', 45]
    ]
)

# processing
a = a - 1
df['age'] = df['age'] - 10

Code used in the extra debug console:

for age_hosei in [10, -10]:
    print('--------------------------')
    print('age_hosei', age_hosei)

    tmp = df.copy()
    print('tmp:\n', tmp)
    
    tmp['age'] = tmp['age'] + age_hosei
    result = tmp.groupby('gender')['age'].sum()
    print('result:\n', result)

Recommended Posts