簡體   English   中英

如何在Power BI中使用Python腳本實現插補?

[英]How to implement imputation with Python script in Power BI?

我正在嘗試運行經過驗證的Python腳本來在PowerBI中輸入數據。 數據最初在Power BI中合並,然后導出到Excel,使用Python進行估算和分析。

現在,我想將Python中的代碼用於Power BI的查詢編輯器,這樣我就可以將插入的數據直接插入到Power BI中並使用其可視化,但是我得到了錯誤。

我嘗試在Power BI中粘貼Python中的相同代碼 - 我認為語法可能存在問題。

dataset=#"PreviousStep"

import pandas as pd

byISO = dataset.groupby(['country ISO'])
byIG = dataset.groupby(['WBG Income Group'])
bytIG = dataset.groupby(['WBG Income Group','Year'])
bytR = dataset.groupby(['UN Sub-Region','Year'])

#Country-level
#Filling up and down
dataset[['col1','col2']] = byISO[['col1','col2']].fillna(
        method='ffill')
dataset[['col1','col2']] = byISO[['col1','col2']].fillna(
        method='bfill')
#Interpolation
dataset[['col1','col2']] = byISO[['col1','col2']]\
         .apply(lambda i: i.interpolate(method='linear', limit_area='inside'))
#Extrapolation (FILLING DOWN CURRENTLY)
dataset[['col1','col2']] = byISO[['col1','col2']]\
         .apply(lambda i: i.interpolate(method='linear', limit_area='outside'))
#Median
dataset[['col1','col2']] = byISO[['col1','col2']]\
    .transform(lambda i: i.fillna(i.median()))

#Group-level
#Median
dataset[['col1','col2']] = byIG[['col1','col2']]\
    .transform(lambda i: i.fillna(i.median()))
#Yearly median
dataset[['col1','col2']] = bytIG[['col1','col2']]\
    .transform(lambda i: i.fillna(i.median()))

#Region-level
#Yearly median
dataset[['col1','col2']] = bytR[['col1','col2']]\
    .transform(lambda i: i.fillna(i.median()))
#No level (All)
#0
dataset[['col1','col2']].fillna(0)

我希望有一個帶有估算值的表,但我得到了這個錯誤:

DataSource.Error: ADO.NET: Python script error.
Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 2, in <module>
    import os, pandas, matplotlib.pyplot
  File "C:\Users\GEscamilla\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']

Details:
    DataSourceKind=Python
    DataSourcePath=Python
    Message=Python script error.
Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 2, in <module>
    import os, pandas, matplotlib.pyplot
  File "C:\Users\GEscamilla\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']

    ErrorCode=-2147467259
    ExceptionType=Microsoft.PowerBI.Scripting.Python.Exceptions.PythonScriptRuntimeException

如果您查看錯誤輸出,它會告訴您

ImportError: Missing required dependencies ['numpy']

這意味着你必須像@prathik在評論中所說的那樣導入numpy和你的其他import語句。 你可以在這里找到microsoft的例子

import numpy

如果這不起作用,您需要確保需要安裝

pip install numpy

更大的圖景

您應該考慮將腳本放在儀表板之前 - 以便轉換后的數據也可以被其他儀表板使用。

通常我會建議在數據倉庫或特定目的的市場中進行所有數據轉換。 然而,這一切都取決於這是一次性練習還是你將要在制作中使用的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM