I'm aware that this similar question has been asked; however, I'm looking for further clarification to have better understanding of .groupby if it's possible. Data used
I want the exact same result like this but with .groupby():
df.pivot(columns='survived').age.plot.hist()

So I try:
df.groupby('age')['survived'].count().plot.hist()

The x-axis doesn't look right. Is there any way I can get the same result as .pivot() does using the pure .groupby() method? Thank you.
Expanding on Quang's comment, you would want to bin the ages rather than grouping on every single age (which is what df.groupby('age') does).
One method is to cut the age bins:
df['age group'] = pd.cut(df.age, bins=range(0, 100, 10), right=False)
Then groupby those bins and make a bar plot of the survived.value_counts():
(df.groupby('age group').survived.value_counts()
.unstack().plot.bar(width=1, stacked=True))
I noticed that in the link you posted, all the histograms look a little different. I think that's due to slight differences in how each method is binned. One advantage of cutting your own bins is that you can clearly see the exact bin boundaries:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With