Forum Discussion
jmarcrum
6 years agoHelper II
Slicers as input for Python script
Hi everyone - I have a python network script visual in Power BI. I am looking to apply a slicer (instead of hard-coding the teams - tm1 and tm2 ) in the python script below. How can be this be ...
- 6 years ago
Hey jmarcrum ,
this code is maybe doing what you are looking for:
# 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(TmName, OppName, Selection1, Selection2) # dataset = dataset.drop_duplicates() # Paste or type your script code here: import networkx import matplotlib.pyplot as plt tm1_l = list(dataset.Selection1) tm1 = tm1_l[1] tm2_l = list(dataset.Selection2) tm2 = tm2_l[1] # load edges file from csv left = list(dataset.TmName) right = list(dataset.OppName) graph = networkx.Graph() graph.add_edges_from(list(zip(left, right))) # Test single path shortest_path = networkx.shortest_path(graph, tm1, tm2) edges_2team = dataset[dataset.TmName.isin(shortest_path)] left_2team= list(edges_2team['TmName']) right_2team = list(edges_2team['OppName']) graph_2team = networkx.Graph() graph_2team.add_edges_from(list(zip(left_2team, right_2team))) plt.title(tm1 + " " + tm2) networkx.draw(graph_2team, with_labels=True) plt.show()as it allows to create this chart:
Be aware of this line:
networkx.draw(graph_2team, with_labels=True)
Hopefully, this is what you are looking for.Regards,
Tom
Greg_Deckler
6 years agoCommunity Champion
I could be mistaken but I am pretty sure that python visuals interact with other visuals so a slicer should pre-filter the dataframe coming into the python visual...
jmarcrum
6 years agoHelper II
I guess i don't really want to filter the dataframe coming in. I really want to pass in a parameter... like allow the user to select a team 1 and a team 2 that I can then pass in to the python script and use in place of the tm1 and tm2 hard-coded values.
Does that make sense? How would I do that?