如何解决未定义数据框列名称 PowerBI Python 集成
我编写了使用 python Jupiter notebook 可视化 matplotlib 条形图的代码。但现在我想将该代码与 powerBI 集成。该数据集包括 3 列(Day-Shift、State 和 seconds)。但它给出了以下错误。为了解决这个错误,我阅读了很多 powerBi 社区解决方案。我更改了不同的 python 版本(现在我使用的是 python 3.5 )并重新安装了 powerBI。但一切都没有改变。
我的代码:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
import pandas as pd
dataset = pd.DataFrame(Day-Shift,State,seconds)
#df = pd.DataFrame({'Day-Shift': table[:,0],'seconds': table[:,1],'State': table[:,2]})
df=dataset
import collections
orderedDict = collections.OrderedDict()
from collections import OrderedDict
# Get all the possible states and associate a color to each of them
all_states = df.State.unique()
cm = plt.get_cmap('tab20') # you can choose the colormap you want
colors = {
s: cm(1. * i / len(all_states)) # get a different color for each state,sampling the color map
for i,s in enumerate(all_states)
}
fig,ax = plt.subplots(1,1)
day_shifts = df['Day-Shift'].unique()
# Plot the bar of each shift independently,so preserving the order of the stack
for i,d in enumerate(day_shifts):
total_height = [0] # total height of the stacked bars so far
# stack each state on top of the previous ones
for t in df[df['Day-Shift'] == d].itertuples():
ax.bar((i,),(t.seconds),bottom=total_height,color=colors[t.State],label=t.State,linewidth=2,edgecolor='w')
total_height = [total_height[0] + t.seconds]
# Add xticks with labels
ax.set_xticks(list(range(len(day_shifts))))
ax.set_xticklabels(day_shifts,rotation=45,ha='right')
# Create an unique legend,removing duplicates
handles,labels = ax.get_legend_handles_labels()
by_label = OrderedDict(zip(labels,handles))
plt.legend(by_label.values(),by_label.keys(),bbox_to_anchor=(1,1))
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [10,5]
# dataset = dataset.drop_duplicates()
# Paste or type your script code here:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。