简体   繁体   English

如何从power bi中的python函数返回单个数据帧

[英]How to return a single dataframe from python function in power bi

I am trying to write a python function that returns only one dataframe in power bi.我正在尝试编写一个 python 函数,它在 power bi 中只返回一个数据帧。 I have multiple data frames in my python script but I only want one dataframe to be returned or printed in my power bi environment.我的 python 脚本中有多个数据帧,但我只想在我的 power bi 环境中返回或打印一个数据帧。

This is my trial, but for some reason, it is not returning anything while trying to connect.这是我的试用版,但由于某种原因,它在尝试连接时没有返回任何内容。

import pandas as pd
import numpy as np

def my_func():

    df=[["d","t","u","y","e"],["d",np.nan,np.nan,np.nan,"o"],["y","p","p","w","r"]]
    df1=[["d","t","u","y","e"],["d",np.nan,np.nan,np.nan,"o"],["y","p","p","w","r"]]
    df=pd.DataFrame(df)
    df1=pd.DataFrame(df1)

    return df

my_func()

在此处输入图片说明

在此处输入图片说明 I was expecting this script to return df but it is not returning anything.我期待这个脚本返回df但它没有返回任何东西。 Is there anything I am missing?有什么我想念的吗? Can anyone help me please?有人可以帮我吗? What am I missing and why it is not working?我错过了什么,为什么它不起作用?

However, this works fine when i dont use function.但是,当我不使用函数时,这很好用。

在此处输入图片说明 在此处输入图片说明

I am just wondering why this is not working when I use function and return.我只是想知道为什么当我使用函数并返回时这不起作用。

The answer:答案:

Change this:改变这个:

my_func()

To this:对此:

output = my_func()

That's assuming you're not trying to do this through a Py Visual in the PowerBI desktop:假设您没有尝试通过 PowerBI 桌面中的 Py Visual 执行此操作:

The details:细节:

As far as I know, to utilize Python within PowerBI you'll need to output a pandas dataframe, and only a pandas dataframe (Unless you're using a Python visual object . Which it seems that you're not).据我所知,要在 PowerBI 中使用 Python,您需要输出一个 Pandas 数据帧,并且需要输出一个 Pandas 数据帧(除非您使用的是Python visual object 。似乎您不是)。 It would be easier to give a concise answer if you had specified how you're running Python here.如果您在此处指定了如何运行 Python,那么给出简洁的答案会更容易。 But I can show you an example that verifies my initial statement using your snippet in the Power Query Editor.但我可以向您展示一个示例,该示例使用您在 Power Query 编辑器中的代码段验证我的初始语句。 For all necessary details on that, please take a look at Power BI: How to use Python with multiple tables in the Power Query Editor?有关所有必要的详细信息,请查看Power BI:如何在 Power Query 编辑器中对多个表使用 Python? . .

Having fired up the Power Query Editor, select Get Data and click OK to insert an emtpy table.启动 Power Query 编辑器后,选择“ Get Data并单击“ OK以插入一个空表。 Then go to Transform and select Run Python Script .然后转到Transform并选择Run Python Script In the following dialog box, insert your script and run it so you'll get this setup:在下面的对话框中,插入您的脚本并运行它,以便您获得此设置:

在此处输入图片说明

And then click Table right next to output to get your dataframe:然后单击output旁边的Table以获取您的数据框:

在此处输入图片说明

If you click Table next to dataset you'll get nothing.如果单击dataset旁边的Table ,您将一无所获。 And that's because you've started out with an empty dataframe.那是因为您从一个空的数据框开始。 If your Python script is part of a more elaborate data loading process, then dataset will contain the data that your Python script can build on.如果您的 Python 脚本是更复杂的数据加载过程的一部分,那么dataset将包含您的 Python 脚本可以构建的数据。 So, what was the answer again?那么,答案又是什么呢? Without output you won't get access to your dataframe.没有output您将无法访问您的数据框。 And without output = myFunc() then you're not letting PowerBI know that you've built a dataframe using Python.如果没有output = myFunc()那么你就不会让 PowerBI 知道你已经使用 Python 构建了一个数据框。 And then all you'll have available in the Power Query Editor is this:然后你将在 Power Query 编辑器中可用的是:

在此处输入图片说明

I hope this makes sense now.我希望这现在有意义。 Don't hesitate to let me know if not.如果没有,请不要犹豫,让我知道。

Edit编辑

You don't have to name the dataframe output in output = my_func() .您不必在output = my_func()命名数据帧output You can call it anything you want.你可以随意称呼它。 Any pandas dataframe that is built in your code snippet will become available in this table:在您的代码片段中构建的任何 Pandas 数据框都将在此表中可用:

在此处输入图片说明

If your goal is to import it in power bi try to change last line as如果您的目标是在 power bi 中导入它,请尝试将最后一行更改为

print(my_func()) as per microsoft instruction按照微软指令print(my_func())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM