Power BI:多个表作为 Power Query 中 Python 的输出
Power BI: Multiple tables as output of Python in Power Query
我是 运行 Power BI 的 Power Query 编辑器中的一个 Python 脚本,用于转换和处理我的数据。在这些计算之后,我想 return 数据集和另一个 table 到 Power Query 编辑器。我是否正确认为第二个 table 需要是一个 Pandas 数据框?
仅当将第二个 table 从 numpy.ndarray 转换为 pandas.DataFrame 时,Power BI 才会将其识别为表格输出 (result after script)。这是预期的行为吗? Pandas 数据帧以外的其他 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
插入空的 table。然后使用 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
中的第一个片段,您将看到 table 显示在 Applied Steps
下哪个变量可用:
dataset
是在插入Python片段时默认构造的,而var2
和var4
是在代码中构造的。都是数据框。即使 var5
是 pandas Series
也无法进一步编辑。
希望对您有所帮助。如果没有,请随时告诉我!
编辑:
关于:
After these computations, I want to return the dataset and another
table to the Power Query editor.
您可以加载任何 table 并使用 Python 进行编辑。如果您想保留 table 的一个版本,并在另一个 table 上做进一步的编辑,您应该看看
我是 运行 Power BI 的 Power Query 编辑器中的一个 Python 脚本,用于转换和处理我的数据。在这些计算之后,我想 return 数据集和另一个 table 到 Power Query 编辑器。我是否正确认为第二个 table 需要是一个 Pandas 数据框?
仅当将第二个 table 从 numpy.ndarray 转换为 pandas.DataFrame 时,Power BI 才会将其识别为表格输出 (result after script)。这是预期的行为吗? Pandas 数据帧以外的其他 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
插入空的 table。然后使用 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
中的第一个片段,您将看到 table 显示在 Applied Steps
下哪个变量可用:
dataset
是在插入Python片段时默认构造的,而var2
和var4
是在代码中构造的。都是数据框。即使 var5
是 pandas Series
也无法进一步编辑。
希望对您有所帮助。如果没有,请随时告诉我!
编辑:
关于:
After these computations, I want to return the dataset and another table to the Power Query editor.
您可以加载任何 table 并使用 Python 进行编辑。如果您想保留 table 的一个版本,并在另一个 table 上做进一步的编辑,您应该看看