简体   繁体   中英

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. 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. 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. 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? to make the following table avaibable on your PowerBI Desktop:

在此处输入图像描述

Then insert a Python Visual and drag both ID and City over to the visual to make the data available to your Python script:

在此处输入图像描述

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. Now you can filter your Python visual using that slicer:

在此处输入图像描述

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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