在 Power BI 中编辑用作数据条目的 python 脚本 [英] Edit python script used as Data entry in Power BI

查看:47
本文介绍了在 Power BI 中编辑用作数据条目的 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本并用它在 Power BI 中创建一个数据框.

I have a python script and used it to create a dataframe in Power BI.

现在我想在 Power BI 中编辑该数据框,但不要从头开始作为新数据输入,因为我想将所有图表保留在我的 Power BI 模型中.

Now I want to edit that dataframe in Power BI but don´t enter from scratch as new data because I want to keep all the charts inside my Power BI model.

例如,在我的旧数据框中,我在脚本中指定了一些日期,因此信息仅限于这些日期.现在我想将日期更改为新日期,但不想丢失所有模型.

For example in my old dataframe i specified some dates inside my script so the information was limited to those dates. Now i want to change the dates to new ones but dont want to lose all the model.

df = df

推荐答案

希望你不要在 PowerBI Python Visual 中搞这个.如果您在 Power Query 编辑器的转换"选项卡下使用 Python,则问题的关键不在于 Python 本身,而在于如果您在 Power Query 编辑器中右键单击查询下的表,您可以使用的参考函数:

I hope you're not dong this in a PowerBI Python Visual. If you're using Python under the Transform tab in the Power Query Editor, the key to your problem lies not in Python itself, but rather in the reference function available to you if you right-click the table under queries in the Power Query Editor:

试试这个:

1: 将以下示例数据保存为 C:pbidatasrc.csv 文件并使用 Get Data > 将其加载到 PowerBI 中.文本/CSV

1: Save the following sample data in a csv as C:pbidatasrc.csv file and load it into PowerBI using Get Data > Text/Csv

A,B,C
1,1*0,100
2,20,200
3,30,300

2:将其显示为表格:

3:通过Edit Queries

4:添加一些 Python

4: Add some Python

在这里,您可以在 Applied steps 下的 Changed type 步骤之后插入一个 Python 片段,其中 Transform >运行 Python 脚本.插入以下示例代码:

Here you can insert a Python snippet after the Changed type step under Applied steps with Transform > Run Python Script. Inserting the following example code:

# 'dataset' holds the input data for this script
import pandas as pd
df=dataset.copy(deep=True)
df['D']=df['C']*2

...会给你这个:

5:假设您现在对此感到满意,并且希望在 Power BI Desktop 上重新绘制图表.我正在使用一个聚集的条形图来得到这个:

5: And let's say that you're happy with this for now and that you'd like to make a plot out of it back on the Power BI Desktop. I'm using a clustered bar chart to get this:

6:现在,就像您说的那样,如果您想改用 df['D']=df['C']/4,但保留相同的数据集,Python脚本和图Plot 1, Py script 1,回到Power Query Editor,使用我开头提到的功能:

6: Now, like you're saying, if you'd like to have df['D']=df['C']/4 instead, but retain the same dataset, Python script and figure Plot 1, Py script 1, go back to the Power Query Editor and use the functionality that I mentioned in the beginning:

7:并添加另一个 Python 代码段:

7: And add another Python snippet:

# 'dataset' holds the input data for this script
import pandas as pd
df=dataset.copy(deep=True)
df['D']=df['D']/4

我们开始了:

现在您有两个不同的 Python 代码片段,它们构建在同一个数据集上.您仍然拥有来自第一个片段的数据,并且您可以使用第二个片段做任何您想做的事情,而不会弄乱您的数据源.

Now you have two different Python snippets that build on the same dataset. You still have the data from the first snippet, and you can do whatever you want with the second snippet without messing up your data source.

8:插入另一个图表进行验证:

8: Insert another chart to verify:

9:也许可以通过更改源文件来获得一些乐趣:

9: Maybe have some fun with the whole thing by changing the source file:

数据:

A,B,C
100,10,100
2,20,200
3,30,150

新情节:

这篇关于在 Power BI 中编辑用作数据条目的 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆