gpt4 book ai didi

python - 电源 BI : Multiple tables as output of Python in Power Query

转载 作者:行者123 更新时间:2023-11-28 17:58:06 26 4
gpt4 key购买 nike

我在 Power BI 的 Power Query 编辑器中运行 Python 脚本来转换和处理我的数据。在这些计算之后,我想将数据集和另一个表返回到 Power Query 编辑器。我是否正确认为第二张表需要是 Pandas Dataframe?

仅当将第二个表从 numpy.ndarray 转换为 pandas.DataFrame 时,Power BI 才会将其识别为表格输出 (result after script) .这是预期的行为吗?除了 Pandas Dataframe 之外的其他 Python 对象是否可以传递到 Power BI?

最佳答案

Am I correct that this second table needs to be a Pandas Dataframe?

Is this the expected behaviour?

是的,但为什么呢?

您可以使用 Python 构建许多变量类型,要求 Power BI 识别所有这些类型将是一个很大的要求。而不是让 Power BI 识别某些变量类型,开发人员似乎为了简单起见决定在 DataFrames 处划清界线。就个人而言,我认为这是一个明智的决定。这样,如果出现任何问题,您就会知道这不是数据类型问题。


一些细节:

转到 Power Query 编辑器并使用 Enter Data > OK 插入一个空表。然后使用 Transform > Run Python Script 插入下面的脚本:

# 'dataset' holds the input data for this script
import numpy as np
import pandas as pd
var1 = np.random.randint(5, size=(2, 4))
var2 = pd.DataFrame(np.random.randint(5, size=(2, 4)))
var3 = 3
var4 = pd.DataFrame([type(var3)])
var5 = pd.Series([type(var3)])

此代码段构造了以下类型的 5 个变量:

print(type(var1))
<class 'numpy.ndarray'>

print(type(var2))
<class 'pandas.core.frame.DataFrame'>

print(type(var3))
<class 'int'>

print(type(var4))
<class 'pandas.core.frame.DataFrame'>

print(type(var5))
<class 'pandas.core.series.Series'>

具体来说,我没有在 PowerBI 中运行 print() 命令,而是在 Spyder 中。现在,如果您单击 OK 并执行 The Power Query Editor 中的第一个代码片段,您将看到一个表格,显示在 下您可以使用哪个变量>应用步骤:

enter image description here

dataset是defult在插入Python代码片段时构造的,而var2var4是在代码中构造的。都是数据框。即使是 pandas Seriesvar5 也无法进一步编辑。

希望对您有所帮助。如果没有,请随时告诉我!

编辑:

关于:

After these computations, I want to return the dataset and another table to the Power Query editor.

您可以加载任何表格并使用 Python 对其进行编辑。如果您想保留一个版本的表,并在另一个表上做进一步的编辑,您应该看看 Edit python script used as Data entry in Power BI

关于python - 电源 BI : Multiple tables as output of Python in Power Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57196327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com