'Dataframe Column name not defined PowerBI Python Integration
i wrote code to visualize matplotlib bar chart using the python Jupiter notebook. But now I wanted to integrate that code with powerBI. That dataset includes 3 columns(Day-Shift, State, and seconds). But it gives the following error. To solve this error I read a lot of powerBi community solutions. I changed different python versions(now I'm using python 3.5 ) and i reinstalled powerBI. But nothing changed.
my code:
# 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:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|