Create an N-Gram Ranking in Power BI | by Ednalyn C. De Dios | Towards Data Science

In a previous article, I wrote a quick start guide on creating and visualizing n-gram ranking using nltk for natural language processing. However, I needed a way to share my findings with others who don’t have Python or Jupyter Notebook installed in their machines. I needed to use our organization’s BI reporting tool: Power BI.
Enter Python Visual.
The Python visual allows you to create a visualization generated by running Python code. In this post, we’ll walk through the steps needed to visualize the results of our n-gram ranking using this visual.
First, let’s get our data. You can download the sample dataset here. Then, we could load the data into Power BI Desktop as shown below:
Select Text/CSV and click on “Connect”.
Select the file in the Windows Explorer folder and click open:
Click on “Load”.
Next, find the Py icon on the “Visualizations” panel.
Then, click on “Enable” at the prompt that appears to enable script visuals.
You’ll see a placeholder appear in the main area and a Python script editor panel at the bottom of the dashboard.
Select the ‘text’ column on the “Fields” panel.
You’ll see a predefined script that serves as a preamble for the script that we’re going to write.
In the Python script editor panel, place your cursor at the end of line #6 and hit enter twice.
Then, copy and paste the following code:
import reimport unicodedataimport nltkfrom nltk.corpus import stopwordsADDITIONAL_STOPWORDS = ['covfefe']import matplotlib.pyplot as pltdef basic_clean(text):  wnl = nltk.stem.WordNetLemmatizer()  stopwords = nltk.corpus.stopwords.words('english') + ADDITIONAL_STOPWORDS  text = (unicodedata.normalize('NFKD', text)    .encode('ascii', 'ignore')    .decode('utf-8', 'ignore')    .lower())  words = re.sub(r'[^\w\s]', '', text).split()  return [wnl.lemmatize(word) for word in words if word not in stopwords]words = basic_clean(''.join(str(dataset['text'].tolist())))bigrams_series = (pandas.Series(nltk.ngrams(words, 2)).value_counts())[:12]bigrams_series.sort_values().plot.barh(color='blue', width=.9, figsize=(12, 8))plt.show()
In a nutshell, the code above transforms extracts n-grams from the 'text' column of thedataset dataframe and creates a horizontal bar graph out of it using matplotlib. The result of plt.show() is what Power BI displays on the Python visual.
For more information on this code, please visit my previous tutorial.
towardsdatascience.com
After you’re done pasting the code, click on the “play” icon at the upper right corner of the Python script editor panel.
After a few moments, you should now be able to see the horizontal bar graph like the one below:
And that’s it!
With a few simple clicks of the mouse, along with some help from our Python script, we’re able to visualize the results of our n-gram ranking.
I hope you enjoyed today’s post on one of Power BI’s strongest features. Power BI already has some useful and beautiful built-in visuals but sometimes, you just need a little bit more flexibility. Running Python code helps with this. I hope this gentle introduction will encourage you to explore more and expand your repertoire.
In the next article, I’ll share a quick-start guide to extracting named-entities from a Pandas dataframe using spaCy.
Stay tuned!
You can reach me on Twitter or LinkedIn.