Integrating Python with Power BI
While working on a project about integrating Python with Power BI recently, I decided to write a blog post about it.
Python is a high level, object-oriented programming language that is used for Data science, Web development and Scripting. With Python you can perform quick data import, data transformation and data visualizations in Power BI.
There are three ways to integrate Python with Power BI
- Create Python visualizations using Python script editor
- Run Python script to import data as datasource to Power BI
- Use Python script to transform data in Query Editor
Today’s post is about creating Python visualizations using Python script editor in Power BI.
These are the steps I used to plot python visualizations in Power BI
- Install Python
- Install python packages
- Install Power BI Desktop
- Import data
- Confirm Python home directory in Power BI Desktop
- Enable Python visuals in Power BI
- Plot the Visualizations
- Install Python for windows or any other Operating system you are using.
2. Install python packages
Launch Command Prompt with Admin Privileges.
Type cd\ at the prompt and Enter
Locate your Python Scripts path. The script folder can be found where Python is installed on your machine. For this example, my Script path is:
Type cd in the prompt followed by the script path and Enter.
Now install the packages you need for your visuals, for this example I am using two packages:
matplotlib and seaborn
At the prompt type pip install package name and enter
To uninstall package
3. Download and install Power BI Desktop
4. Import data into Power BI Desktop.
For this example, I am using CSV file. From File > Get Data > Text/CSV > Connect > Select file > Load
5. Confirm Python home directory in Power BI Desktop.
This path should match your Python installation path on your Machine.
File > Options and Settings > Options > Python Scripting > Detected Path home directories.
6. Enable Python visuals in Power BI
Click Py under Visualizations panel , click Enable
7. Plot the Visualizations
Select the Columns you want for the visualization under the Fields Panel
When you select the Fields, Power BI automatically creates a dataframe called dataset in the python script editor. The dataframe is comprised of all the different columns selected under fields panel. You can access a column by its name (dataset[“A12”]) in Python script.
Now you can write your Python script in the space provided and click Run Script to see the visual.
Happy scripting 🙂
One thought on “Integrating Python with Power BI”
Your post is very helpful for me to install python packages and have it work in Power BI
Comments are closed.