Script python in power bi

Запустить сценарий Python в Power BI Desktop

Вы можете выполнять скрипты Python непосредственно в Power BI Desktop и импортировать полученные наборы данных в модель данных Power BI Desktop. На основе этой модели можно создавать отчеты и передавать их в службу Power BI для общего доступа.

Предварительные требования

  • Для запуска сценариев Python в Power BI Desktop необходимо установить Python на локальном компьютере. Скачать Python можно с веб-сайта Python. В текущем выпуске скриптов Python поддерживаются символы Юникода и пробелы в пути установки.
  • Для интеграции Power BI с Python требуется установка следующих двух пакетов Python. В консоли или оболочке используйте программу командной строки pip для установки пакетов. Средство pip упаковано с последними версиями Python.
    • Pandas — это библиотека программного обеспечения для обработки и анализа данных. Pandas предлагает структуры данных и операции для управления числовыми таблицами и временными рядами. Для импорта в Power BI данные Python должны находиться в кадре данных Pandas. Кадр данных — это двумерная структура данных, например таблица со строками и столбцами.
    • Matplotlib — это библиотека построения графиков для Python и его расширение numPy для числовой математики. Matplotlib предоставляет объектно-ориентированный API для внедрения графиков в приложения графического пользовательского интерфейса общего назначения (GUI) для Python, такие как Tkinter, wxPython, Qt или GTK+.
    pip install pandas pip install matplotlib 

    Включение скриптов Python

    Чтобы включить скрипты Python в Power BI, выполните следующие действия.

    Снимок экрана: параметры скрипта Python для Power BI Desktop.

    1. В Power BI Desktop последовательно выберите Файл>Параметры и настройки>Параметры>Создание скриптов Python. Откроется страница Параметры скриптов Python.
    2. При необходимости укажите или измените локальный путь установки Python в разделе Обнаруженные домашние каталоги Python. На предыдущем рисунке локальный путь установки Python — C:\Python. Если у вас есть несколько локальных установок Python, обязательно выберите ту, которую вы хотите использовать.
    3. Щелкните ОК.

    Power BI запускает скрипты непосредственно с помощью исполняемого файлаpython.exe из каталога, который вы указали в разделе Параметры. Дистрибутивы Python, требующие дополнительного шага для подготовки среды, например Conda, могут завершиться сбоем. Чтобы избежать этих проблем, используйте официальный дистрибутив Python из https://www.python.org. Другим возможным решением является запуск Power BI Desktop из пользовательской командной строки среды Python.

    Создание скрипта Python

    Создайте скрипт в локальной среде разработки Python и убедитесь, что он успешно выполняется. Для подготовки и запуска скрипта Python в Power BI Desktop существует несколько ограничений.

    • Импортируются только кадры данных Pandas, поэтому убедитесь, что данные, которые вы хотите импортировать в Power BI, представлены в кадре данных.
    • Время ожидания любого скрипта Python, выполняющегося дольше 30 минут, истекает.
    • Интерактивные вызовы в скрипте Python, такие как ожидание ввода данных пользователем, приостанавливают выполнение скрипта.
    • Если вы задали рабочий каталог в скрипте Python, необходимо определить полный путь к рабочему каталогу, а не относительный путь.
    • Вложенные таблицы не поддерживаются.

    Ниже приведен простой пример скрипта Python, который импортирует Pandas и использует кадр данных:

    import pandas as pd data = [['Alex',10],['Bob',12],['Clarke',13]] df = pd.DataFrame(data,columns=['Name','Age'],dtype=float) print (df) 

    При запуске этот скрипт возвращает:

     Name Age 0 Alex 10.0 1 Bob 12.0 2 Clarke 13.0 

    Запуск скрипта и импорт данных

    Чтобы запустить скрипт Python, выполните следующие действия.

    1. В группе Главная на ленте Power BI Desktop выберите Получить данные.
    2. В диалоговом окне Получение данных выберите Другой>скрипт Python, а затем щелкните Подключиться. Power BI использует последнюю установленную версию Python в качестве подсистемы Python. Снимок экрана: получение данных с выбранным скриптом Python.
    3. На экране Скрипт Python вставьте скрипт Python в поле Скрипт и нажмите кнопку ОК. Снимок экрана: вставка примера скрипта Python в диалоговое окно скрипта Python.
    4. Если скрипт выполняется успешно, появится окно Навигатор , и вы сможете загрузить данные. Выберите таблицу df , а затем щелкните Загрузить. Снимок экрана: окно навигатора с данными для загрузки и использования.

    Power BI импортирует данные, и их можно использовать для создания визуализаций и отчетов. Чтобы обновить данные, выберите Обновить в группе Главная ленты Power BI Desktop. При обновлении Power BI снова запустит скрипт Python.

    Если Python не установлен или не определен, появится предупреждение. Кроме того, при наличии нескольких установок на локальном компьютере может появиться предупреждение.

    Снимок экрана: предупреждение о том, что Python не установлен.

    Дальнейшие действия

    Дополнительные сведения о Python в Power BI см. в статье:

    Источник

    Create Power BI visuals with Python

    This tutorial helps you get started creating visuals with Python data in Power BI Desktop. You use a few of the many available options and capabilities for creating visual reports by using Python, pandas, and the Matplotlib library.

    Prerequisites

    • Install Python on your local machine.
    • Enable Python scripting in Power BI Desktop.
    • Install the pandas and Matplotlib Python libraries.
    • Import the following Python script into Power BI Desktop:
    import pandas as pd df = pd.DataFrame(< 'Fname':['Harry','Sally','Paul','Abe','June','Mike','Tom'], 'Age':[21,34,42,18,24,80,22], 'Weight': [180, 130, 200, 140, 176, 142, 210], 'Gender':['M','F','M','M','F','M','M'], 'State':['Washington','Oregon','California','Washington','Nevada','Texas','Nevada'], 'Children':[4,1,2,3,0,2,0], 'Pets':[3,2,2,5,0,1,5] >) print (df) 

    Create a Python visual in Power BI Desktop

    1. After you import the Python script, select the Python visual icon in the Power BI Desktop Visualizations pane. Screenshot that shows the Python option in Visualizations.
    2. In the Enable script visuals dialog box that appears, select Enable. A placeholder Python visual image appears on the report canvas, and the Python script editor appears along the bottom of the center pane. Screenshot that shows the Python script editor.
    3. Drag the Age, Children, Fname, Gender, Pets, State, and Weight fields to the Values section where it says Add data fields here. Screenshot that shows Drag to Add data fields here.Based on your selections, the Python script editor generates the following binding code.
      • The editor creates a dataset dataframe with the fields you add.
      • The default aggregation is Don’t summarize.
      • Similar to table visuals, fields are grouped and duplicate rows appear only once.
    4. With the dataframe automatically generated by the fields you selected, you can write a Python script that results in plotting to the Python default device. When the script is complete, select the Run icon from the Python script editor title bar to run the script and generate the visual. Screenshot that shows the Python script editor with initial comments.

    Tips

    • Your Python script can use only fields that are added to the Values section. You can add or remove fields while you work on your Python script. Power BI Desktop automatically detects field changes. As you select or remove fields from the Values section, supporting code in the Python script editor is automatically generated or removed.
    • In some cases, you might not want automatic grouping to occur, or you might want all rows to appear, including duplicates. In those cases, you can add an index field to your dataset that causes all rows to be considered unique and prevents grouping.
    • You can access columns in the dataset by using their names. For example, you can code dataset[«Age»] in your Python script to access the age field.
    • Power BI Desktop replots the visual when you select Run from the Python script editor title bar, or whenever a data change occurs due to data refresh, filtering, or highlighting.
    • When you run a Python script that results in an error, the Python visual isn’t plotted, and an error message appears on the canvas. For error details, select See details in the message.
    • To get a larger view of the visualizations, you can minimize the Python script editor.

    Create a scatter plot

    Create a scatter plot to see if there’s a correlation between age and weight.

      In the Python script editor, under Paste or type your script code here, enter this code:

    import matplotlib.pyplot as plt dataset.plot(kind='scatter', x='Age', y='Weight', color='red') plt.show() 

    Your Python script editor pane should now look like the following image: Screenshot that shows the Python script editor with commands.The code imports the Matplotlib library, which plots and creates the visual.

  • Select the Run script button to generate the following scatter plot in the Python visual. Screenshot that shows the scatter plot visualization generated from the Python script.
  • Create a line plot with multiple columns

    Create a line plot for each person that shows their number of children and pets.

      Under Paste or type your script code here, remove or comment out the previous code, and enter the following Python code:

    import matplotlib.pyplot as plt ax = plt.gca() dataset.plot(kind='line',x='Fname',y='Children',ax=ax) dataset.plot(kind='line',x='Fname',y='Pets', color='red', ax=ax) plt.show() 

    Screenshot that shows a line plot with multiple columns from the Python script.

  • Select the Run button to generate the following line plot with multiple columns:
  • Create a bar plot

    Create a bar plot for each person’s age.

      Under Paste or type your script code here, remove or comment out the previous code, and enter the following Python code:

    import matplotlib.pyplot as plt dataset.plot(kind='bar',x='Fname',y='Age') plt.show() 

    Screenshot that shows a bar plot from the Python script.

  • Select the Run button to generate the following bar plot:
  • Limitations

    Python visuals in Power BI Desktop have the following limitations:

    • The data the Python visual uses for plotting is limited to 150,000 rows. If more than 150,000 rows are selected, only the top 150,000 rows are used, and a message appears on the image. The input data also has a limit of 250 MB.
    • If the input dataset of a Python visual has a column that contains a string value longer than 32,766 characters, that value is truncated.
    • All Python visuals display at 72 DPI resolution.
    • If a Python visual calculation exceeds five minutes, the execution times out, which results in an error.
    • As with other Power BI Desktop visuals, if you select data fields from different tables with no defined relationship between them, an error occurs.
    • Python visuals refresh upon data updates, filtering, and highlighting. The image itself isn’t interactive.
    • Python visuals respond to highlighting elements in other visuals, but you can’t select elements in the Python visual to cross-filter other elements.
    • Only plots to the Python default display device display correctly on the canvas. Avoid explicitly using a different Python display device.
    • Python visuals don’t support renaming input columns. Columns are referred to by their original names during script execution.

    Security

    Python visuals use Python scripts, which could contain code that has security or privacy risks. When you attempt to view or interact with a Python visual for the first time, you get a security warning. Enable Python visuals only if you trust the author and source, or after you review and understand the Python script.

    Licensing

    Python visuals require a Power BI Pro or Premium Per User (PPU) license to render in reports, refresh, filter, and cross-filter. Users of free Power BI can consume only tiles that are shared with them in Premium workspaces.

    The following table describes Python visuals capabilities based on licensing.

    Author Python visuals in Power BI Desktop Create Power BI service reports with Python visuals View Python visuals in reports
    Guest (Power BI embedded) Supported Not supported Supported in Premium/Azure capacity only
    Unmanaged tenant (domain not verified) Supported Not supported Not supported
    Managed tenant with free license Supported Not supported Supported in Premium capacity only
    Managed tenant with Pro or PPU license Supported Supported Supported

    For more information about Power BI Pro licenses and how they differ from free licenses, see Purchase and assign Power BI Pro user licenses.

    Next steps

    This tutorial barely scratches the surface of the options and capabilities for creating visual reports using by Python, pandas, and the Matplotlib library. For more information, see the following resources:

    For more information about Python in Power BI, see:

    Источник

    Читайте также:  Contains operator in java
Оцените статью