繁体   English   中英

Power BI 和 Python 不对齐

[英]Power BI and Python not aligned

我是 Power BI 的新手,但不是 python / pandas 的新手。Power BI 会如何更改传递给 python 脚本编辑器的值,我如何在 Power BI 的脚本/设置中考虑它?

如果我必须在 power BI 中手动抓取 excel 个文件,这在某种程度上完全违背了使用它的目的。

这是场景:我创建了 a.groupby() pivot 表并将其绘制在 python 中。我得到了我预期的图表。 当我将脚本移至 Power BI python 脚本编辑器时,我得到了一个明显不正确的图表。 它只有 3 个数据点,看起来它是在做一个表总和而不是按平均值分组? 值 go 到 1E6

为了澄清,当我在选择要访问的字段后使用 Power BI 创建的 dataframe 时,会生成不正确的图表。 核心数据源完全相同。 如果我抓取原始的 CSV,我可以重新创建原始的正确图形。

这是 Power BI 创建的图形的图片。

在此处输入图像描述

这是接近我期望的东西

在此处输入图像描述

我在 python 和 Power BI python 脚本编辑器中都使用了以下代码。

#The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

#dataset = pandas.DataFrame(Color, ValueB, Value_A, ID, StepTime, Bucket)
#dataset = dataset.drop_duplicates()

#Paste or type your script code here:
#assumptions: all returned email columns are named
import sys
import time

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

df1 = dataset  # Power BI specific and returns the funky graph
df1 = pd.read_excel # Works in both power BI and Python


base = 100
print(df1.head())
df1['Value_A'] = df1['Value_A'].round(0).astype('Int64')
df1['Buckets'] = (df1['Value_A']/base).round(0)*base
df1['Buckets'] = df1['Buckets'].round(0).astype('Int64')

df2 = df1.groupby(['ID','Buckets'])['ValueB'].mean().reset_index()

df2 = df2.loc[df2['Buckets'] > 5]
group_19 = df2.loc[df2['ID'] == 19]
group_67 = df2.loc[df2['ID'] == 67]
group_69 = df2.loc[df2['ID'] == 69]


#Plotting
plt.scatter(group_19['Buckets'], group_19['ValueB'],c = 'blue')
plt.plot(group_19['Buckets'],group_19['ValueB'],ls='-',marker = 'o', c = 'blue')

plt.scatter(group_67['Buckets'], group_67['ValueB'],c = 'green')
plt.plot(group_67['Buckets'],group_67['ValueB'],ls='-',marker = 'o', c = 'green')

plt.scatter(group_69['Buckets'], group_69['ValueB'],c = 'red')
plt.plot(group_69['Buckets'],group_69['ValueB'],ls='-',marker = 'o', c = 'red')



plt.show()

答案比我想象的要简单。 Power BI 会在您加载数据时自动汇总数据。我必须手动选择“不汇总”才能获得正确的原始数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM