Home > OS >  How do i get my bars in my df.plot() to be one column and the rest of the columns to be on the x axi
How do i get my bars in my df.plot() to be one column and the rest of the columns to be on the x axi

Time:11-28

i have this dataframe:

    Week      Income    Fuel  ...  Extras  Total costs  Remainder
 18-18-218    2000.0  1200.0  ...   122.0       1842.0      158.0
 12-12-2012   1750.0   100.0  ...   100.0        820.0      930.0
    nan       1786.0   289.0  ...   109.0       1060.0      726.0

What i have now is:

chartdf = pd.DataFrame(chartlist)
chartdf.plot.bar(x="Week", rot=0)
plt.show()

enter image description here

And what i would like is:

for the bars in my plot.bar() to be the column "week" and then the x axis to the other columns headers. So that instead of it showing 9 bars in 3 categories, it shows 3 bars(weeks) and 9 categories. The data is just dummydata to see if it works thats why the dates are weird haha.

CodePudding user response:

you need to change your data so you set the Week as an index and drop it from the columns then you create the plot from the transpose of the data and it will give you your expected output:

chartdf=chartdf.set_index('Week', drop=True)
                    
chartdf.T.plot.bar(rot=0)

as an example of results:

enter image description here

  • Related