Make a Plot Bar from Pandas multilevel dataframe

Question:

I’m having troubles trying to make a bar plot from a pandas dataframe, which should be easy but I can’t make it work.
I have a dataframe that looks like that:

Data A Data B Data C
timestamp
06:54:00 0.1 0.2 0.3

But instead of 3 columns with Data, I have 99.
The point is that I am trying to do a bar plot representing in the x axis the different Data and in the y axis the values.

I tried with:

p = data.hvplot.bar(x = 'Data', y = 'Units', rot = 90)

And

p = data.plot(kind='bar', title="Data", figsize=(15, 10), legend=True, fontsize=12)

But none of them are working, and I think that the problem comes from the format of my dataframe, because of the column ‘timestamp’.
However, I haven’t manage to delete it, I tried:

data = data.droplevel('timestamp')

And:

data = data.drop(['timestamp'], axis=1)

But none of them are working. Could someone please give me a hand with that?

Asked By: Sara.SP92

||

Answers:

You can try this:

new_df = df.melt(var_name="Data")
new_df.plot(kind='bar', x='Data', y='value')

enter image description here

Answered By: cazman

I finally managed to solve it.

What I did was:

new_df = data.melt(var_name="Data")

To get a new dataframe without the timestamp.
Then:

titles = new_df['Data'].to_list()

values = new_df['value'].to_list()

To get two lists, one with the titles and another one with the values.

And then I plotted the chart with the following code:

p = figure(x_range=titles, height=500, width=1500, title="Unit",
           toolbar_location=None, tools="")

p.vbar(x=titles, top=values, width=0.6)

p.xgrid.grid_line_color = None
p.xaxis.major_label_orientation = "vertical"
p.y_range.start = 0

Thank you all,

Answered By: Sara.SP92
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.