0

I'm trying to plot three dataframe based on their Total Activity Hours Per Month so I used the groupby function.

df110 = df[df['Attendance Abs Type Code'] == '110']
df120 = df[df['Attendance Abs Type Code'] == '120']

dfWithout110 = df[df['Attendance Abs Type Code'] != '110 ']
dfWithout110120 = dfWithout110[dfWithout110['Attendance Abs Type Code'] != '120']

dfWithout110120Chart = dfWithout110120[["Activity Hours"]].groupby(dfWithout110120["Activity Month"]).sum().plot(kind='bar', width=0.8, title="Total activity hours per month (Without 110 & 120)")
df110chart = df110[["Activity Hours"]].groupby(df110["Activity Month"]).sum().plot(kind='bar', width=0.8, title="Total activity hours per month (110 only)")
df120chart = df120[["Activity Hours"]].groupby(df120["Activity Month"]).sum().plot(kind='bar', title="Total activity hours per month (120 only)")

Download link for the dataset https://drive.google.com/file/d/1YdSsP8BM4PVNh8m2kW7244NOo75lNUT7/view?usp=sharing

Here is some sample data from the dataframe:

enter image description here

Please take a look at the attached picture.

enter image description here

What I really wanted is actually a stacked barchart like this:

enter image description here

2
  • Can you include a copy-pastable version of your data? Commented Oct 21, 2019 at 3:48
  • Alright I'll upload my dataset csv Commented Oct 21, 2019 at 4:11

1 Answer 1

1

You can use the hue parameter in seaborn:

import seaborn as sns

df['hue'] = df["Attendance Abs Type Code"]
df.loc[~df['hue'].isin(['110', '120']), 'hue'] = 'Other'
df = df.groupby(['Activity Month', 'hue'])['Activity Hours'].sum().reset_index()

sns.barplot(x='Activity Month', y='Activity Hours', hue='hue', data=df)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi. It's working but I'm not really sure about the correctness of the result. The graph for Other should be higher right? Since the total monthly activity hours for the "Other" is much higher than the other two (110 and 120).
@FirdhausSaleh You need to groupby and sum first.
I tried to code them but I failed. Would u mind helping me with code to groupby and sum first please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.