power python_在 Power BI Desktop 中运行 Python 脚本
在 Power BI Desktop 中運行 Python 腳本Run Python scripts in Power BI Desktop
06/02/2020
本文內容
你可以直接在 Power BI Desktop 中運行 Python 腳本,并將結果數據集導入 Power BI Desktop 數據模型。You can run Python scripts directly in Power BI Desktop and import the resulting datasets into a Power BI Desktop data model.
安裝 PythonInstall Python
若要在 Power BI Desktop 中運行 Python 腳本,需要在本地計算機上安裝 Python。To run Python scripts in Power BI Desktop, you need to install Python on your local machine. 可以從Python 網站下載 Python。You can download Python from the Python website. 當前的 Python 腳本版本支持在安裝路徑中包含 Unicode 字符和空格。The current Python scripting release supports Unicode characters and spaces in the installation path.
安裝所需的 Python 包Install required Python packages
Power BI Python 集成需要安裝兩個 Python 包:The Power BI Python integration requires the installation of two Python packages:
用于數據操作和分析的軟件庫。A software library for data manipulation and analysis. 它提供了用于處理數值表和時序的數據結構和操作。It offers data structures and operations for manipulating numerical tables and time series. 導入的數據必須位于 pandas 數據幀中。Your imported data must be in a pandas data frame. 數據幀是一種二維數據結構。A data frame is a two-dimensional data structure. 例如,數據按表格的形式分布在行和列中。For example, data is aligned in a tabular fashion in rows and columns.
Python 的繪圖庫及其數值數學擴展 NumPy。A plotting library for Python and its numerical mathematics extension NumPy. 它提供了面向對象的 API,可使用通用 GUI 工具包(例如 Tkinter、wxPython、Qt 或 GTK+)將繪圖嵌入到應用程序中。It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits, such as Tkinter, wxPython, Qt, or GTK+.
在控制臺或 shell 中,使用 pip 命令行工具安裝這兩個包。In a console or shell, use the pip command-line tool to install the two packages. Pip 工具與最新的 Python 版本打包在一起。The pip tool is packaged with more recent Python versions.
pip install pandas
pip install matplotlib
啟用 Python 腳本Enable Python scripting
要啟用 Python 腳本,請執行以下操作:To enable Python scripting:
在 Power BI Desktop 中,依次選擇“文件” > “選項和設置” > “選項” > “Python 腳本” 。In Power BI Desktop, select File > Options and settings > Options > Python scripting. 你將看到“Python 腳本選項”頁面。The Python script options page appears.
如有必要,請在“檢測到的 Python 主目錄”中指定本地 Python 安裝路徑。If necessary, specify your local Python installation path in Detected Python home directories.
在上圖中,Python 的安裝本地路徑為 C:\Python。In the above image, the Python's installation local path is C:\Python. 確保該路徑用于希望 Power BI Desktop 使用的本地 Python 安裝。Make sure the path is for the local Python installation you want Power BI Desktop to use.
選擇“確定”。Select OK.
指定 Python 安裝后,即可開始在 Power BI Desktop 中運行 Python 腳本。Once you specify your Python installation, you’re ready to begin running Python scripts in Power BI Desktop.
運行 Python 腳本Run Python scripts
只需幾個步驟,即可運行 Python 腳本并創建數據模型。In just a few steps, you can run Python scripts and create a data model. 在本模型中,可以創建報表并在 Power BI 服務上共享它們。From this model, you can create reports and share them on the Power BI service.
準備 Python 腳本Prepare a Python script
首先,請在本地 Python 開發環境中創建腳本并確保其成功運行。First, create a script in your local Python development environment and make sure it runs successfully. 例如,下面是一個簡單的 Python 腳本,用于導入 pandas 并使用數據幀:For example, here's a simple Python script that imports pandas and uses a data frame:
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)
print (df)
運行時,此腳本返回:When run, this script returns:
Name Age
0 Alex 10.0
1 Bob 12.0
2 Clarke 13.0
在 Power BI Desktop 中準備和運行 Python 腳本時,會有一些限制:When preparing and running a Python script in Power BI Desktop, there are a few limitations:
由于僅導入 Pandas 數據幀,因此請確保要導入到 Power BI 的數據都以數據幀表示Only pandas data frames are imported, so make sure the data you want to import to Power BI is represented in a data frame
任何 Python 腳本若運行時間超過 30 分鐘就會超時Any Python script that runs longer than 30 minutes times out
Python 腳本中的交互式調用(如等待用戶輸入)會終止腳本執行Interactive calls in the Python script, such as waiting for user input, halts the script’s execution
在 Python 腳本中設置工作目錄時,必須定義工作目錄的完整路徑,而非相對路徑When setting the working directory within the Python script, you must define a full path to the working directory, rather than a relative path
當前不支持嵌套表Nested tables are currently not supported
運行 Python 腳本并導入數據Run your Python script and import data
要在 Power BI Desktop 中刷新 Python 腳本,請執行以下操作:To run your Python Script in Power BI Desktop:
在“主頁”功能區中,選擇“獲取數據” > “其他”。In the Home ribbon, select Get Data > Other.
選擇“其他” > “Python 腳本” ,如下圖所示:Select Other > Python script as shown in the following image:
選擇“連接”。Select Connect. 選擇本地最新安裝的 Python 版本作為 Python 引擎。Your local latest installed Python version is selected as your Python engine. 將腳本復制到顯示的“Python 腳本”對話框中。Copy your script into the Python script dialog box that appears. 在這里,我們輸入之前顯示的簡單 Python 腳本。Here, we enter the simple Python script shown before.
選擇“確定”。Select OK. 如果腳本成功運行,則會顯示“導航器”,你可以加載數據并使用它。If the script runs successfully, the Navigator appears and you can load the data and use it. 對于本示例,如圖所示,選擇“df”,然后選擇“加載”。For the example, select df, as shown in the image, then Load.
故障排除Troubleshooting
如果未安裝或未標識 Python,系統將顯示警告。If Python isn't installed or identified, a warning displays. 如果有多個本地計算機安裝,系統也會顯示一條警告。You can also see a warning if you have multiple local machine installations. 重新回顧并查看之前的“安裝 Python 并啟用 Python 腳本”部分。Revisit and review the previous Install Python and Enable Python scripting sections.
使用自定義 Python 分發Using custom Python distributions
Power BI 直接通過從用戶提供的目錄(通過設置頁面提供)使用 python.exe 可執行文件來執行腳本。Power BI executes scripts directly by using the python.exe executable from a user-provided directory (provided through the settings page). 如果分發需要額外的步驟來準備環境(例如 Conda),則可能會遇到執行失敗的問題。Distributions that require an extra step to prepare the environment (for example, Conda) might encounter an issue where their execution fails.
為避免相關問題,建議使用來自 https://www.python.org/ 的官方 Python 分發。We recommend using the official Python distribution from https://www.python.org/ to avoid related issues.
一個可能的解決方案是,你可從自定義 Python 環境提示處啟動 Power BI Desktop。As a possible solution, you can start Power BI Desktop from your custom Python environment prompt.
刷新Refresh
你可以在 Power BI Desktop 中刷新 Python 腳本。You can refresh a Python script in Power BI Desktop. 若要刷新,請轉到“主頁”功能區,然后選擇“刷新” 。To refresh, go to the Home ribbon and select Refresh. 刷新 Python 腳本時,Power BI Desktop 會再次運行 Python 腳本。When you refresh a Python script, Power BI Desktop runs the Python script again.
已知限制Known Limitations
目前,你無法在創建時啟用了增強型元數據(預覽版)功能的報表中使用 Python 腳本。Currently you won't be able to use Python scripts in reports created with Enhanced Metadata (Preview) feature enabled. 現有報表將繼續發揮作用。Existing reports will continue to work.
后續步驟Next steps
查看以下更多信息,了解有關 Power BI 中的 Python。Take a look at the following additional information about Python in Power BI.
總結
以上是生活随笔為你收集整理的power python_在 Power BI Desktop 中运行 Python 脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows睡眠(休眠)唤醒助手_一劳
- 下一篇: python中numeric_Pytho