Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
vincent_lu
Regular Visitor

how to get & process data from & using python scripts

I want to get data from local files which will be processed by Python scripts in PBI desktop, using the following steps:

Open PBI Desktop -> Get Data -> Other -> Python scripts -> entering scripts in the new window.

Question: What kinds of scripts I should write which can return some variants that can be used in PBI visuals

when I wrote the following scripts, I can get nothing from the scripts:

a=1

b=2

 

what I expected is that I can use var a and b for some new visuals, just like I getting the data from the following csv file:

a b

1 2

 

1 ACCEPTED SOLUTION
siricher
Regular Visitor

 

The Python connector will import the DataFrame variables defined in your script.

For instance, the following code generates the table you've described:

 

ab
12

 

dataset = pandas.DataFrame([[1,2]], columns = [ 'a', 'b' ])


If you want to import csv files, you can use panda's read_csv function: 

dataset = pandas.read_csv("C:\\path\\to\\my\\data.csv")

(However, if you all you want is import csv files, you can also use the "Text/Csv" data source from Get Data -> File)

View solution in original post

4 REPLIES 4
siricher
Regular Visitor

 

The Python connector will import the DataFrame variables defined in your script.

For instance, the following code generates the table you've described:

 

ab
12

 

dataset = pandas.DataFrame([[1,2]], columns = [ 'a', 'b' ])


If you want to import csv files, you can use panda's read_csv function: 

dataset = pandas.read_csv("C:\\path\\to\\my\\data.csv")

(However, if you all you want is import csv files, you can also use the "Text/Csv" data source from Get Data -> File)

thanks a lot, this works.

 

The whole code in Python would be like this

import pandas as pd
dataset = pd.DataFrame([[1,2]], columns = [ 'a', 'b' ])

 

And Power BI creates a Power Query wrapper around it, automagically

 

let
Source = Python.Execute("import pandas as pd#(lf)dataset = pd.DataFrame([[1,2]], columns = [ 'a', 'b' ])"),
dataset1 = Source{[Name="dataset"]}[Value],
#"Changed Type" = Table.TransformColumnTypes(dataset1,{{"a", Int64.Type}, {"b", Int64.Type}})
in
#"Changed Type"

 

What I could not figured out, how to edit Python code in IDE, for example in VS Code or Sublime.

I've tried to list IDE in Options, but Power BI Desktop keeps using a simple internal so called "Advanced Editor"

 

Regarding your comments about csv... Well, I have not tested it yet, but the integration with Python would resolve one of the weakest point of PBD for me - quick reload of datasets, where cleansing thru Pandas or Power Query is needed before I can work with it in DAX and visual DAX.  I use a lot of Parquet in my Pandas workflow. So, if the load speed of Parquet will be the same in Python script inside of Power BI that could be a game changer, especially for datasets > 100M. Using Evo 960 I can get amazing load speeds in Pandas. Will test and post back.

 

Update - tested, now I can use Parquet and Feather formats to upload data into Power BI. This opens some new ways to use PBD in my workflows.

Can you confirm if loading using Feather format is faster since it's already a Tabular format which is memory optimized?

 

Parquet is better for reduction of disk space, not memory like Feather.

Anonymous
Not applicable

@vyacheslavg 

To edit the python script in power bi, i.e once the script is run and data is loaded and you want to edit the script then Go to edit queries > select the table and on the right-hand side in applied steps click on the source setting it will open the python script. You can edit your python script here.

 

Cheers.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.