Skip to content

Use GitHub Copilot in VS Code for Python and Pandas Scripts

For Statisticians ·

Tool:VS Code
AI Feature:Copilot inline suggestions and Copilot Chat
Time:15-20 minutes
Difficulty:Beginner
GitHub Copilot

What This Does

Where RStudio's Copilot assistant handles R work, the GitHub Copilot extension in VS Code covers the Python side of a mixed-language workflow: pandas reshaping and merging, statsmodels or scipy scaffolding, and the setup code every new analysis script needs before the real work starts. Inline suggestions complete a line as you type. Copilot Chat, a separate panel, takes a plain-language request and writes a larger block or an entire function. The same subscription covers both surfaces.

Before You Start

  • VS Code installed (free, code.visualstudio.com)
  • A GitHub account
  • A Pro subscription ($10/month): github.com/features/copilot
  • The Python extension for VS Code installed
  • Confirmation that your organization's approved-tools list covers Copilot for project code, not just for data. A pandas script that reshapes claims or trial data can itself reveal a study design or a rate structure even with no data in the file, so check before you point it at anything tied to a live project

Steps

1. Install the Copilot extension and sign in

Open the Extensions view (the icon with four squares in the left sidebar, or Ctrl+Shift+X), search "GitHub Copilot," and install the extension published by GitHub. Click the account icon in the bottom-left corner, choose Sign in with GitHub, and authorize VS Code in the browser window that opens.

2. Accept inline suggestions as you write pandas code

Open a .py file and start a familiar operation:

Copy and paste this
# merge the adverse events table with the demographics table on subject_id,
# keeping only subjects present in both

Copilot proposes the pandas merge call as gray text ahead of your cursor. Press Tab to accept it in full, or the right arrow to take it a word at a time. It adapts to the naming conventions already in your file, so results get better as the script fills in.

3. Use Copilot Chat for a full scaffold

For something bigger than one line, open Copilot Chat (the chat icon in the sidebar, or Ctrl+Shift+I) and describe the task directly: "Write a Python function using scipy.stats that runs a two-sample t-test on two numeric columns, returns the statistic, p-value, and a 95 percent confidence interval for the difference in means, and handles missing values by listwise deletion." Copilot writes the function in the chat panel with an Insert at Cursor option to drop it into your file.

4. Ask it to explain code you did not write

Highlight a block of unfamiliar statsmodels or pandas code, open Copilot Chat, and ask "explain this" or "what does this groupby chain produce." This is often faster than the alternative here, tracing through pandas documentation for a chained method call you inherited from someone else.

5. Check every generated line against what you actually intended

A merge with the wrong join type silently drops or duplicates rows without raising an error, and a statsmodels call with a default argument you did not notice can quietly change what is being estimated. Run the generated code against a small test case with a known answer before trusting it on a real dataset.

Real Example

Scenario: You are scaffolding a new script to check whether a metric differs by experiment arm, starting from raw event-level data that needs to be aggregated to the subject level first.

What you type in Copilot Chat: "Write a pandas script that aggregates an events dataframe to one row per subject_id (sum of event_count), merges it with an arm assignment dataframe on subject_id, then runs a Mann-Whitney U test comparing the two arms using scipy.stats."

What you get: A working script with the groupby aggregation, the merge, and the scipy.stats.mannwhitneyu call, commented and ready to point at your actual column names.

Time comparison: Writing this scaffold from scratch: 15-25 minutes, mostly remembering exact pandas and scipy syntax. Reviewing and adjusting the generated version: 5 minutes.

Tips

  • Inline suggestions work best when your file already has a few lines of similar code to pattern-match against. Copilot Chat is the better starting point for a blank file
  • Copilot sends the open file's surrounding content, and sometimes other open files, to GitHub as context for its suggestions. Keep proprietary algorithm logic or protocol text out of comments even in a Python file that never touches real data
  • Ask Copilot Chat to add docstrings to a function you just accepted. It is a quick way to get documentation you would otherwise skip under deadline

Tool interfaces change. If a button has moved, look for similar AI/magic/smart options in the same menu area.