如何使用Python在PowerBI中制作可重现的数据样本?

背心

这是一个自我回答的帖子。为什么?因为缺少数据样本,所以Power BI中的许多问题都无法回答。另外,许多人似乎想知道如何使用Python在Power BI中编辑数据表。当然,全世界都需要Power BI中Python的更广泛使用。有人认为您必须将Python代码段应用于其他位置加载的现有表。我对本文的回答将向您展示如何在原本为空的Power BI文件中用几行代码构建一个(相当大的)数据样本。

因此,如何在Power BI中使用Python构建数据样本并对其进行更改?

背心

我将向您展示如何构建10000包含分类值和数值的数据集我将Python库numpypandas分别用于数据生成和表操作。下面的代码片段仅从两个列表10000时间中绘制了一个随机元素,以构建带有一些街道和城市名称的两列,并将一个随机数列表添加到混合中。然后,我使用熊猫将数据组织到数据框中。在中使用Python Power BI Power Query Editor,您的输入必须是表格,而输出则必须是pandas数据框。

Python片段:

import numpy as np
import pandas as pd

np.random.seed(123)
streets=['Broadway', 'Bowery', 'Houston Street']
cities=['New York', 'Chicago', 'Baltimore']

rows = 1000

lst_cities=np.random.choice(cities,rows).tolist()
lst_streets=np.random.choice(streets,rows).tolist()
lst_numbers= np.random.randint(low=0, high=100, size=rows).tolist()
df_dataset=pd.DataFrame({'City':lst_cities,
                      'Street':lst_streets,
                      'ID':lst_numbers})
df_metadata = pd.DataFrame([df_dataset.shape])

Power BI:

在Power BI Desktop中,单击Enter Data以转到Power Query Editor在下面的对话框窗口中,除了单击,什么都不要做OK结果是一个空表和下面的两个步骤Applied steps

在此处输入图片说明

现在,使用Transform > Run Python Script,在上面插入代码段,然后单击OK以获取以下信息:

在此处输入图片说明

You now have a preliminary table with 2 columns and 3 rows. And this is a pretty neat detail of the implementation of Python in Power BI. These are three different datasets that are made available to you after running your snippet. Dataset is constructed by default, but is empty since we started out with an empty table. If we started out with some other data, the first line of the Run Python Script explains the purpose of this table # 'dataset' holds the input data for this script. And it is constructed in the form of a pandas dataframe. The last table df_metadata is only a brief description of the dataset we're really interested in: df_dataset, but I've added it to the mix in order to illustrate that all dataframes made by you in your snippet will be available to you. You chose which table to continue working on by clicking Table next to the name.

在此处输入图片说明

就是这样!现在,您有了一个混合数据类型表,可以继续使用Python或Power BI本身进行工作:

在此处输入图片说明

在这里您可以:

  1. 使用任何菜单选项继续在桌子上工作
  2. 插入另一个Python脚本
  3. 复制原始数据Reference框,Table通过右键单击下的创建一个,继续使用其他版本Queries

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Julia中提供可重现的样本数据

如何使样本和lapply的组合可重现?

如何制作可重现的Apache Spark示例

如何在R中以可重现数据产生NA

如何制作出色的R可重现示例

如何使用 set.seed() 和 sample() 减少用于创建可重现数据帧的代码?

如何使用文档数据在python中制作2维数组

如何使用数据存储在变量中的python制作雷达图

Python-如何在不创建文件的情况下制作CSV数据样本?

如何使用Java Sound中的音频样本数据?

如何使用R中的样本获取数据帧

如何使用 bootstrap 样本数据在 spss 中绘制直方图?

使用python从数据框中随机选择子样本

Java 中的可重现加密(可能使用 Jasypt)

如何在python中从每个值都有不同样本大小的数据框中过滤出随机样本?

如何在 BigQuery 中显示数据样本?

如何从数据库中检索样本?

如何计算R数据帧中事件的重现率

如何使用乌龟在Python中制作烟花

如何使用从中导入数据的文件名来命名pandas DataFrame中的数据样本?

如何制作使用 input() 从文本文件中获取输入数据的 python 代码?

如何使用列名作为字符串的数据框在python中制作稀疏矩阵

如何使用不同的系列在python中制作数据框?

如何为我的angularjs / jasmine单元测试提供可重复使用的样本数据值

如何使用Presto SQL获取可重复的样本?

如何创建可重现的随机数

如何创建可重现的 SpatRaster (terra) 示例?

奇怪的 410 http 使用 python urllib 在 wget 中无法重现

使用rpy2在python中重现R glmnet代码