Group bar chart after using .loc and .groupby in pandas

Viewed 93

Need to draw a grouped bar chart of the following data set.The volume of name product in each location on daily data set.For the group bar chart, x-axis needs to be week and y-axis needs to volume and used group by to name and hue needs the location.

new.loc[(new['Volume'] !=0)].groupby('name')['Volume','location','day'].plot(kind='bar',x='week',y='Volume')

data

After using the above code I got a chart look like below,barchart

But I need a graph look like below.What change do i need to make to convert the code to grouped bar chart? data

1 Answers

If using pandas's plotting doesn't work, are you opposed to using Seaborn and Matplotlib.pyplot? If not, you can try something like this:

########### Create mock dataframe ##############
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
Volume = [0, 17, 0, 48, 52, 69, 8, 10, 20]
name = ["Mike", "Mike", "Steve", "Mike", "John", "John", "John", "Mark", "Mark"]
location = ["Front", "Back", "Back", "Front", "Back", "Front", "Front", "Back", "Front"]
day = ['Monday', 'Tuesday', 'Tuesday', 'Friday', 'Thursday', 'Thursday', "Friday", 'Monday', 'Monday']
new = pd.DataFrame({'Volume': Volume,
                    'location': location,
                    'name':name,
                    'day':day})
#################################################

new = new.loc[(new['Volume'] !=0)]

for i in set(new.name):
    sns.barplot(x="day", y="Volume", data=new[new.name == i], hue="location")
    plt.title(i)
    plt.show()

enter image description here enter image description here enter image description here

Related