在Power BI Desktop中运行python时出错以下是python脚本和运行脚本时出现的错误消息

问题描述

Python脚本 __ #'dataset'保存此脚本的输入数据

import requests
import json
import pandas as pd

username= 'abcuser' #enter Jasper Username
api_key= '1-2-3' #enter Jasper API Key

iccid =pd.DataFrame(dataset.loc[:,'Ship File Recon.SIM iccid'])
jasperURL = 'https://URL/'
counter = 0
Header = pd.DataFrame(columns=['iccid','imsI','Sim Status','Session Status','IP Address','Session End Date'])
print(Header)
for row in iccid.itertuples(index=False,name=None):
    index = str(row).replace("(","")
    index = index.replace(")","")
    index = index.replace("L","")
    index = index.replace(",","")
    test = jasperURL + index
    sessionStatus = jasperURL + index + '/sessionInfo'
    counter = counter + 1
    a = '' #iccid
    b = '' #imsI
    c = '' #Sim Status
    d = '' #session status
    e = '' #ip address
    f = '' #date Session Ended
    jasperResponse = requests.get(test,auth=(username,api_key))
    if (jasperResponse.ok):
        jasperData = json.loads(jasperResponse.content)
        a = jasperData['iccid']
        b = jasperData['imsi']
        c = jasperData['status']
    else:
        a = 'N/A'
        b = 'N/A'
        c = 'N/A'
    sessionStatusResponse = requests.get(sessionStatus,api_key))
    if (sessionStatusResponse.ok):
        sessionData = json.loads(sessionStatusResponse.content)
        if (sessionData['dateSessionStarted'] is not None and sessionData['dateSessionEnded'] is None):
            d = 'In Session'
            f = 'N/A'
        else:
            d = 'Not in Session'
            f = sessionData['dateSessionEnded']
            #if the sim has never been in session,last connection date will be Null
            if (f is None):
                f = 'No Connection History'
        if (sessionData['ipAddress'] is not None):
            e = sessionData['ipAddress']
        else:
            e = 'Not in Session'
    else:
        d = 'N/A'
        e = 'N/A'
        f = 'N/A'
    iccid = iccid.append({"iccid":a + ' ',"imsI":b + ' ',"Sim Status":c + ' ',"Session Status":d + ' ',"IP Address":e + ' ',"Session End Date":f + ' '},ignore_index=True)

当我尝试在特定表上的Power BI中运行该脚本时,该脚本将失败 错误

DataSource.Error: ADO.NET: Python script error.
Traceback (most recent call last):
  File "C:\PROGRAM FILES\python38\lib\site-packages\pandas\core\indexes\base.py",line 2889,in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx",line 70,in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx",line 97,in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi",line 1675,in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi",line 1683,in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Ship File Recon.SIM iccid'

上述异常是以下异常的直接原因:

Traceback (most recent call last):
  File "PythonScriptWrapper.PY",line 21,in <module>
    iccid =pd.DataFrame(dataset.loc[:,'Ship File Recon.SIM iccid'])
  File "C:\PROGRAM FILES\python38\lib\site-packages\pandas\core\indexing.py",line 873,in __getitem__
    return self._getitem_tuple(key)
  File "C:\PROGRAM FILES\python38\lib\site-packages\pandas\core\indexing.py",line 1044,in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "C:\PROGRAM FILES\python38\lib\site-packages\pandas\core\indexing.py",line 786,in _getitem_lowerdim
    section = self._getitem_axis(key,axis=i)
  File "C:\PROGRAM FILES\python38\lib\site-packages\pandas\core\indexing.py",line 1110,in _getitem_axis
    return self._get_label(key,axis=axis)
  File "C:\PROGRAM FILES\python38\lib\site-packages\pandas\core\indexing.py",line 1059,in _get_label
    return self.obj.xs(label,axis=axis)
  File "C:\PROGRAM FILES\python38\lib\site-packages\pandas\core\generic.py",line 3476,in xs
    return self[key]
  File "C:\PROGRAM FILES\python38\lib\site-packages\pandas\core\frame.py",line 2899,in __getitem__
    indexer = self.columns.get_loc(key)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)