简体   繁体   English

使用 Python 在 Power BI 中过滤 Python Script Visual 中的数据

[英]Filter the data in Python Script Visual in power BI using Python

I have created one basic python script visual in power bi and now I want to apply a filter to that visual using that script only.我在 power bi 中创建了一个基本的 python 脚本视觉对象,现在我想仅使用该脚本对该视觉对象应用过滤器。 Below is the code I have written.下面是我写的代码。

 import pandas
 import matplotlib.pyplot as plt
 a=dataset.Test_name
 plt.bar(a,dataset.Value)
 plt.show()

The data set Test_name contains multiple rows and that's why my graphs are showing multiple bars.数据集Test_name包含多行,这就是我的图表显示多个条形的原因。 Now I only want to see the bar of the data present on the 1 st row or 3 rd row of the dataset Test_name .How can I achieve that in Power BI Python script visual.现在我只想查看数据集Test_name的第一行或第三行上的数据条。如何在 Power BI Python 脚本视觉中实现这一点。 I have used the array functionalities but it causes problems while plotting the graph.我使用了数组功能,但在绘制图形时会导致问题。

Without a data sample the following is the best suggestion I can give:如果没有数据样本,以下是我能给出的最佳建议:


Follow the instructions in How to make a reproducible data sample in PowerBI using Python?按照如何使用 Python 在 PowerBI 中制作可重现的数据样本中的说明进行操作? to make the following table avaibable on your PowerBI Desktop:使下表在您的 PowerBI 桌面上可用:

在此处输入图像描述

Then insert a Python Visual and drag both ID and City over to the visual to make the data available to your Python script:然后插入 Python 视觉对象并将IDCity拖到视觉对象上,以使数据可用于 Python 脚本:

在此处输入图像描述

Insert the following snippet and run it:插入以下代码段并运行它:

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset = pandas.DataFrame(ID, City)
# dataset = dataset.drop_duplicates()

# Paste or type your script code here:
import pandas
import matplotlib.pyplot as plt
#a=dataset.Test_name
plt.bar(dataset['City'], dataset['ID'])
plt.show()

Now you'll have this:现在你会有这个:

在此处输入图像描述

Insert a Slicer and drag ID over to it.插入一个Slicer器并将ID拖到它上面。 Now you can filter your Python visual using that slicer:现在您可以使用该切片器过滤您的 Python 视觉对象:

在此处输入图像描述

Of course, this is not exactly what you're aiming to do here, but it was the best I could do without knowing your data and your data structure.当然,这并不完全是您在这里的目标,但这是我在不知道您的数据和数据结构的情况下能做的最好的事情。 So why is this helpful to you?那么,为什么这对您有帮助? Well, at least you now know how to set up a bar chart and a slicer using Python and Power BI.好吧,至少您现在知道如何使用 Python 和 Power BI 设置条形图和切片器。 And you also know that any errors with this setup will be because of your data.而且您还知道此设置的任何错误都是由于您的数据造成的。

Let me know how it works out for you.让我知道它是如何为你工作的。

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

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