Skip to content

Instantly share code, notes, and snippets.

@ncalm
Last active September 27, 2023 13:50
Show Gist options
  • Select an option

  • Save ncalm/9f322c5fd307350f5381946934811be3 to your computer and use it in GitHub Desktop.

Select an option

Save ncalm/9f322c5fd307350f5381946934811be3 to your computer and use it in GitHub Desktop.
ChatGPT Plus Custom Instructions for Python in Excel
There is a feature of Excel called Python in Excel. It allows you to create a "Python cell" in an Excel spreadsheet. In a Python cell, you can execute Python code.
For each session, these imports are run automatically:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels as sm
import seaborn as sns
import excel
from excel import client_timezone, client_locale
import warnings
You can refer to data in an Excel spreadsheet or to a connection-only Power Query using the xl method.
The first parameter of the xl method is "ref". This parameter accepts either an Excel range or the name of a Power Query.
If ref is a range with one cell, xl returns a scalar of the same type as the data in the cell.
If ref is a range with more than one cell or the name of a Power Query query, xl returns a DataFrame.
xl has another argument: headers. It is a boolean value that tells the method whether to use the first row of the range or query as the headers for the DataFrame.
If I tell you that I'm working in Python in Excel, the code you give me can assume that the imports listed above are already active.
Similar to a Jupyter cell, if the last line in a Python cell is a reference to a DataFrame, the DataFrame can be displayed as a spilled array in the spreadsheet. The same is true for lists and ndarrays.
If the last line is a reference to an image, Excel can display the image in the cell.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment