Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的Python脚本在PowerBI中运行时表现不同_Python_Powerbi - Fatal编程技术网

为什么我的Python脚本在PowerBI中运行时表现不同

为什么我的Python脚本在PowerBI中运行时表现不同,python,powerbi,Python,Powerbi,我正在尝试使用python脚本在PowerBI中创建自定义可视化 我能够使用以下代码生成一个漂亮的图表 # Prolog - Auto Generated # import os, uuid, matplotlib matplotlib.use('Agg') import matplotlib.pyplot import pandas import sys sys.tracebacklimit = 0 strPath='[some path]\\Python\\PythonEditorWrap

我正在尝试使用python脚本在PowerBI中创建自定义可视化

我能够使用以下代码生成一个漂亮的图表

# Prolog - Auto Generated #
import os, uuid, matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot
import pandas

import sys
sys.tracebacklimit = 0
strPath='[some path]\\Python\\PythonEditorWrapper_88cb7f60-a0c9-4e06-b940-5aec52dc6870'
os.chdir(strPath)

dataset = pandas.read_csv('input_df_9549609e-e459-4507-b285-db37256a785c.csv')

matplotlib.pyplot.figure(figsize=(5.55555555555556,4.16666666666667), dpi=72)
matplotlib.pyplot.show = lambda args=None,kw=None: matplotlib.pyplot.savefig(str(uuid.uuid1()))
# Original Script. Please update your script content here and once completed copy below section back to the original editing window #
    
import matplotlib.pyplot as plt
fig, ax = plt.subplots()

dataset = dataset[dataset['Year'] >= 2020]

ds2 = dataset.groupby(['Name'])['Price'].agg(['sum'])
ds2 = ds2.reset_index()

merged_inner = pandas.merge(left=ds2, right=dataset, left_on='Name', right_on='Name')
    
merged_inner['totalSum'] = merged_inner['sum']
ds = merged_inner.groupby(['Name','Quarter','totalSum'])['Price'].agg(['sum'])

ds = ds.sort_values('totalSum',ascending=False)
ds = ds.reset_index()
dfSectors = ds['Name']

dfSectors = dfSectors.drop_duplicates()

print(merged_inner)

dfQtr = dataset['Quarter']
print(ds)
dfQtr = dfQtr.drop_duplicates()
width = 0.35

bottomArr=[0,0,0,0,0,0]
for qtr in dfQtr:
    ax.bar(dfSectors, ds[ds['Quarter'] == qtr]['sum'], width, label=qtr, bottom=bottomArr)
    
    vs = ds[ds['Quarter'] == qtr]['sum']
    i = 0
    for v in vs:
        bottomArr[i] += v
        i += 1
    
plt.show() 
但是当我尝试将相关代码(粘贴在错误下方)粘贴回Power BI时,我得到了以下错误<代码>无法显示此视觉效果。Python脚本错误。KeyError:“totalSum”

我发现引起问题的那一行是

ds = ds.sort_values('totalSum',ascending=False)
为了使这一行起作用,我不得不改变前面的几行

merged_inner['sum'] = merged_inner.groupby(['Name','Quarter','totalSum'])['Price'].agg(['sum'])['sum']
ds = merged_inner
现在可视化运行没有任何问题,但我的可视化是空的

有了上面提到的影响,为什么Python脚本在PowerBI中的运行方式会有所不同? 我希望通过得到这个答案,我将能够修复我的代码,并了解更多有关Power BI的信息

merged_inner['sum'] = merged_inner.groupby(['Name','Quarter','totalSum'])['Price'].agg(['sum'])['sum']
ds = merged_inner