python中power的用法_在 Power BI Desktop 中使用 Python 创建 Power BI 视觉对象 - Power BI | Microsoft Docs...

使用 Python 创建 Power BI 视觉对象Create Power BI visuals by using Python

04/03/2020

本文内容

借助 Power BI Desktop ,可以使用 Python 将数据可视化。With Power BI Desktop, you can use Python to visualize your data.

先决条件Prerequisites

Work through the Run Python scripts in Power BI Desktop tutorial using the following Python script:

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)

在 Power BI Desktop 中运行 Python 脚本一文介绍了如何在本地计算机上安装 Python 并在 Power BI Desktop 中利用它编写 Python 脚本。The Run Python scripts in Power BI Desktop article shows you how to install Python on your local machine and enable it for Python scripting in Power BI Desktop. 本教程使用上述脚本中的数据来说明如何创建 Python 视觉对象。This tutorial uses data from the above script to illustrate creating Python visuals.

在 Power BI Desktop 中创建 Python 视觉对象Create Python visuals in Power BI Desktop

在“可视化效果”窗格中选择“Python 视觉对象”图标 。Select the Python visual icon in the Visualizations pane.

在出现的“启用脚本视觉对象”对话框中,选择“启用” 。In the Enable script visuals dialog box that appears, select Enable.

向报表添加 Python 视觉对象后,Power BI Desktop 执行以下操作:When you add a Python visual to a report, Power BI Desktop takes the following actions:

占位符 Python 视觉对象图像位于报表画布上。A placeholder Python visual image appears on the report canvas.

Python 脚本编辑器 位于中央窗格底部边缘处。The Python script editor appears along the bottom of the center pane.

接下来,将“Age”、“Children”、“Fname”、“Gender”、“Pets”、“State”和“Weight”字段拖到显示“在此处添加数据字段”的“值”部分 。Next, drag the Age, Children, Fname, Gender, Pets, State, and Weight fields to the Values section where it says Add data fields here.

Python 脚本只能使用添加到“值”部分的字段 。Your Python script can only use fields added to the Values section. 在使用 Python 脚本时,可以在“值”部分添加或删除字段 。You can add or remove fields from the Values section while working on your Python script. Power BI Desktop 会自动检测字段更改。Power BI Desktop automatically detects field changes.

备注

Python 视觉对象的默认聚合类型是“不汇总” 。The default aggregation type for Python visuals is do not summarize.

现在你可以使用你选择用来创建绘图的数据。Now you can use the data you selected to create a plot.

在选择或删除字段时,系统会自动生成或删除 Python 脚本编辑器中的支持代码。As you select or remove fields, supporting code in the Python script editor is automatically generated or removed.

基于这些选择,Python 脚本编辑器将生成以下绑定代码。Based on your selections, the Python script editor generates the following binding code.

编辑器创建了一个包含添加字段的“数据集”数据帧 。The editor created a dataset dataframe, with the fields you added.

默认聚合是“不汇总” 。The default aggregation is: do not summarize.

类似于表格视觉对象,字段将进行分组,并且重复行只出现一次。Similar to table visuals, fields are grouped and duplicate rows appear only once.

提示

在某些情况下,你可能不希望进行自动分组,或者可能希望所有行都出现,包括重复项。In certain cases, you might not want automatic grouping to occur, or you'll want all rows to appear, including duplicates. 在这种情况下,你可以向将导致所有行被视为唯一且阻止分组的数据集添加索引字段。If so, you can add an index field to your dataset that causes all rows to be considered unique and which prevents grouping.

可以使用各自的名称访问数据集中的列。You can access columns in the dataset using their respective names. 例如,可以在 Python 脚本中编写 dataset["Age"] 的代码来访问年龄字段。For example, you can code dataset["Age"] in your Python script to access the age field.

借助所选字段自动生成的数据帧,就可以编写 Python 脚本,它将会绘制到 Python 默认设备。With the dataframe automatically generated by the fields you selected, you're ready to write a Python script that results in plotting to the Python default device. 脚本完成后,在“Python 脚本编辑器”标题栏中选择“运行” 。When the script is complete, select Run from the Python script editor title bar.

当以下任一事件发生时,Power BI Desktop 会重新绘制视觉对象:Power BI Desktop replots the visual if any of the following events occur:

当从“Python 脚本编辑器” 标题栏选择“运行” 时When you select Run from the Python script editor title bar

每当数据更改发生时(由于数据刷新、筛选或突出显示所导致)Whenever a data change occurs, due to data refresh, filtering, or highlighting

如果运行 Python 脚本时导致错误,则不会绘制 Python 视觉对象,并且会出现一个画布错误消息。When you run a Python script that results in an error, the Python visual isn't plotted and a canvas error message appears. 有关错误详细信息,请从消息中选择“查看详情” 。For error details, select See details from the message.

若要获取可视化效果的较大视图,你可以尽量减小 Python 脚本编辑器 。To get a larger view of the visualizations, you can minimize the Python script editor.

好的,让我们创建一些视觉对象。Ok, let's create some visuals.

创建散点图Create a scatter plot

让我们创建一个散点图,看看年龄与体重之间是否具有相关性。Let's create a scatter plot to see if there's a correlation between age and weight.

在“粘贴或在此处键入脚本代码”下,输入此代码 :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()

Python 脚本编辑器窗格现在应如下所示:Your Python script editor pane should now look like this:

导入 matplotlib 库以绘制并创建视觉对象 。The matplotlib library is imported to plot and create our visuals.

如果选择“运行”脚本按钮,则会在占位符 Python 视觉对象图像中生成以下散点图 。When you select the Run script button, the following scatter plot generates in the placeholder Python visual image.

创建包含多个列的线条图Create a line plot with multiple columns

让我们为每个用户创建一个显示其子女数和宠物数的线条图。Let's create a line plot for each person showing their number of children and pets. 删除或注释“删除或在此处键入脚本代码”下的代码,并键入以下 Python 代码 :Remove or comment the code under Paste or type your script code here and enter this 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()

如果选择“运行”脚本按钮,则将生成包含多个列的以下线条图 。When you select the Run script button, the following line plot with multiple columns generates.

创建条形图Create a bar plot

让我们创建一个包含每个人年龄的条形图。Let's create a bar plot for each person's age. 删除或注释“删除或在此处键入脚本代码”下的代码,并键入以下 Python 代码 :Remove or comment the code under Paste or type your script code here and enter this Python code:

import matplotlib.pyplot as plt

dataset.plot(kind='bar',x='Fname',y='Age')

plt.show()

如果选择“运行”脚本按钮,则会生成以下条形图 :When you select the Run script button, the following bar plot generates:

安全性Security

重要

Python 脚本安全性: Python 视觉对象是基于 Python 脚本创建的,这可能包含具有安全风险或隐私风险的代码。Python scripts security: Python visuals are created from Python scripts, which could contain code with security or privacy risks. 当尝试查看 Python 视觉对象或首次与其交互时,用户会看到一条安全警告消息。When attempting to view or interact with an Python visual for the first time, a user is presented with a security warning message. 仅当你信任作者和来源,或者在查看并了解 Python 脚本之后,才启用 Python 视觉对象。Only enable Python visuals if you trust the author and source, or after you review and understand the Python script.

有关使用 Matplotlib、Pandas 和 Python 绘图的详细信息More information about plotting with Matplotlib, Pandas, and Python

本教程旨在帮助用户开始在 Power BI Desktop 中使用 Python 创建视觉对象。This tutorial is designed to help you get started creating visuals with Python in Power BI Desktop. 它仅仅浅显地介绍了使用 Python、Pandas 和 Matplotlib 库创建视觉对象报表的多种选项和功能。It barely scratches the surface about the many options and capabilities for creating visual reports using Python, Pandas, and the Matplotlib library. 下面提供了很多详细的信息,以及一些入门介绍链接。There's a lot more information out there, and here are a few links to get you started.

已知限制Known limitations

Power BI Desktop 中的 Python 视觉对象有一些限制:Python visuals in Power BI Desktop have a few limitations:

数据大小限制。Data size limitations. Python 视觉对象用于绘制的数据仅限 150,000 行。Data used by the Python visual for plotting is limited to 150,000 rows. 如果选择了 150,000 行以上,则只会使用前 150,000 行,且在图像上显示一条消息。If more than 150,000 rows are selected, only the top 150,000 rows are used and a message is displayed on the image. 此外,输入数据的限制为 250 MB。Additionally, the input data has a limit of 250 MB.

分辨率。Resolution. 所有 Python 视觉对象均以 72 DPI 显示。All Python visuals are displayed at 72 DPI.

计算时间限制。Calculation time limitation. 如果 Python 视觉对象计算时间超过 5 分钟,则执行将超时并生成一个错误。If a Python visual calculation exceeds five minutes the execution times out which results in an error.

关系。Relationships. 如同其他 Power BI Desktop 视觉对象,如果选择的不同表中数据字段间没有定义关系,则会发生错误。As with other Power BI Desktop visuals, if data fields from different tables with no defined relationship between them are selected, an error occurs.

Python 视觉对象在数据更新、筛选和突出显示时进行刷新。Python visuals are refreshed upon data updates, filtering, and highlighting. 但是,图像本身不是交互的并且不能为交叉筛选的源。However, the image itself isn't interactive and can't be the source of cross-filtering.

Python 视觉对象响应突出显示的其他视觉对象,但你不能单击 Python 视觉对象中的元素以进行其他元素的交叉筛选。Python visuals respond to highlighting other visuals, but you can't click on elements in the Python visual to cross filter other elements.

只有绘制到 Python 默认显示设备的绘图会正确地显示在画布上。Only plots that are plotted to the Python default display device are displayed correctly on the canvas. 避免显式使用不同的 Python 显示设备。Avoid explicitly using a different Python display device.

Python 视觉对象不支持对输入列进行重命名。Python visuals do not support renaming input columns. 在脚本执行期间,将按列的原始名称对其进行引用。Columns will be referred to by their original name during script execution.

后续步骤Next steps

查看以下更多信息,了解有关 Power BI 中的 Python。Take a look at the following additional information about Python in Power BI.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值