Published on

July 2, 2022

Getting Data from Python in Power BI

Python is one of the most popular programming languages, and it is widely used in various fields, including data analysis and visualization. SQL Server has integrated features to work with Python, and Power BI is no exception. In this article, we will explore how to get data from Python and send it to Power BI.

Requirements

Before we begin, make sure you have the following:

  • A Windows machine with Power BI Desktop installed
  • Internet access to download the Python installer

Installing Python

Python is a powerful programming language that comes with a wide range of useful libraries for data manipulation and advanced statistical graphics. To install Python, you can visit the official Python website and download the installer.

Enabling Python in Power BI

In order to use Python in Power BI, you need to enable it in the Power BI Desktop. To do this, go to File > Options and settings > Options. Then, navigate to Python scripting and check if the home directory and the detected Python IDEs are correctly set up.

Getting Data from Python in Power BI

Once Python is enabled, you can start getting data from Python in Power BI. To do this, follow these steps:

  1. Click on the “Get data” icon in Power BI.
  2. Type “pyt” to search for the Python script and select it.
  3. Write a script to get the data. For example:
import pandas as pd
data = [['Sylvester','Stallone',76],['Arnold','Schwarzenegger',74]]
actors = pd.DataFrame(data,columns=['Firstname','Lastname','Age'])

Once you press OK, you can select whether to load the data directly, transform the data, or cancel the operation. If everything is fine, you will be able to visualize the data in Power BI.

Loading Data from Python in Power BI using the read_csv Function

In addition to writing scripts to get data, you can also use the pandas library to read data from a CSV file. For example:

import pandas as pd
users = pd.read_csv("c:/data/Import_User_Sample_en.csv")

If everything is OK, you will be able to visualize the data in Power BI. Although Power BI can already import CSV files without Python, using Python allows you to combine code and logic to manipulate the data in more advanced ways.

Working with Conditional Data from Python in Power BI

You can also use Python to work with conditional data in Power BI. For example, you can classify numbers as odd or even based on certain conditions. Here is an example:

import pandas as pd
data = {'list_of_numbers': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
numbers = pd.DataFrame(data)
numbers.loc[numbers['list_of_numbers'] % 2 == 0, 'Odd_Even'] = 'Even'
numbers.loc[numbers['list_of_numbers'] % 2 != 0, 'Odd_Even'] = 'Odd'

This code classifies numbers as odd or even based on whether they can be divided by 2. If the number cannot be divided by 2, it is classified as odd; otherwise, it is classified as even.

Generating Random Numbers with Python in Power BI

Python can also be used to generate random numbers and load them into Power BI. Here is an example:

import random
import pandas as pd
res = [random.randrange(22, 33) for i in range(3)]
randlist = pd.DataFrame(res)

In this example, we are using the random library to generate 3 random values and adding them to a list. These values can then be loaded into Power BI for further analysis.

Conclusion

In this article, we have explored different examples of how to import data from Python into Power BI. We have covered importing static data, reading data from a CSV file, working with conditional data, and generating random numbers. Python is a powerful programming language that can greatly enhance your data analysis and visualization capabilities in Power BI. If you are familiar with Python, it can be a valuable tool to complement your Power BI skills.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.