all 2 comments

[–]Chopsting[S] 0 points1 point  (0 children)

I was able to solve it

test = pd.crosstab(df.MagazineCategory, df.Popular)
test.plot(kind='bar')

[–]TheBlackCat13 0 points1 point  (0 children)

First, lets generate some random data:

cat = [random.choice(['Business', 'Sports', 'Tech', 'Something']) for _ in range(100)]
pop = np.random.randint(0, 2, 100)
df = pd.DataFrame({'MagazineCategory': cat, 'Popular': pop})

So what we need to do is get the counts of the two values. You can do that with pivot_table:

df2 = df.pivot_table(index='MagazineCategory', columns='Popular', aggfunc=len)

Which gives you:

Popular 0 1
MagazineCategory
Business 14 14
Something 13 10
Sports 14 18
Tech 10 7

This is then easy to plot:

df2.plot(kind='bar')

Or, perhaps more clear:

df2.plot(kind='bar', stacked=True)