原書示範讓 Copilot 產生 Python in Excel 程式碼:讀取 A1:G11、建立 Total Spending = Avg Daily Spend × Days Stayed、排序後用 Matplotlib 畫 city bar chart。
PY() 的 Python code。Prompt 指定 PY()、A1:G11、xl(..., headers=True)、Total Spending、降冪排序與 city bar chart。
| 欄位/要求 | 設定 |
|---|---|
| Data range | A1:G11 |
| 衍生欄 | Total Spending |
| 公式概念 | Avg Daily Spend (€) × Days Stayed |
| 排序 | Total Spending descending |
| 圖表 | City vs Total Spending bar chart |
原書 Copilot code 先 df = xl("A1:G11", headers=True),再建立 df["Total Spending"] = df["Days Stayed"] * df["Column1"],排序後以 plt.bar(...) 畫圖。原書表示作者當時 workbook 中這段 code 能正確執行;但 Column1 必須和實際匯入欄名核對。
程式在 D16 以 =PY( 執行後,cell 顯示 [PY] Image;再由 Insert Data → Display Plot over Cells 顯示 Figure 12-6 的最終長條圖。A 檔正文沒有列出每個城市的 Total Spending 精確值。
AI 生成圖表 code 最常見的錯誤不是 Python syntax,而是引用了錯誤/自動改名的欄位。正式使用前應先列出 df.columns,確認 Column1 是否真的是 Avg Daily Spend。
請先讀取 A1:G11 並列出 df.columns,確認哪一欄是 Avg Daily Spend,再建立 Total Spending、降冪排序並畫城市長條圖。若欄名與我描述不同,不要猜,請使用實際 DataFrame 欄名。