Forum Discussion
Collecting data (not visual) outputs from Python
- 7 years ago
Hi sfmike99
when you open a Query in the query editor you can choose Python script in the Transform tab (on the far right).
The DataFrame passed to Python is named 'dataset' and it is the table of the preceding query step. You can apply transofmrations to this DataFrame within Python by inputting the code and after you click ok you will see different DataFrames available for expansion, normally here you'd expand only the dataframe you actually want to return. Let me know if you need me to post pictures which could make it more clear
- 7 years ago
sfmike99 yes all the Drataframes should be visible after the script is run. You should have as many rows as dataframes created by your code. Normally then you'd filter by just the one you'd want to expand and then expand it
Hi LivioLanzo and sfmike99 , this is exactly, what I'm looking for. I understand every step and it works, but now I need my new dataframes to become available outside the query editor.
You just need to name your new dataframe in the Python script, then expand that one when it is finished.
For instance, I have a table with a bunch of programs with survey rounds and scores. I create a new dataframe based on unique program names, then compute the correlation between round and score for each program. (This is a perfect use case for Python since Power BI has very limited stats capabilities).
# 'dataset' holds the input data for this script
import scipy.stats
Results = dataset[['PROGRAM']].drop_duplicates()
Results['INDEX'] = Results['PROGRAM']
Results.set_index('INDEX', inplace=True)
for idx in Results.index:
MeasureX = dataset['ROUND'].loc[dataset['PROGRAM'] == idx]
MeasureY = dataset['SCORE'].loc[dataset['PROGRAM'] == idx]
Output = scipy.stats.pearsonr(MeasureX, MeasureY)
Results.loc[idx, 'R Value'] = Output[0]
Results.loc[idx, 'P Value'] = Output[1]When that completes, I see both 'dataset' and 'Results' coming back - I simply click to expand 'Results' and it's available in my Power BI model like any other table.