EDG956
EDG956

Reputation: 982

Pandas stacked histogram from column of DataFrame by values of another column?

Let's say I have data like this:

Column A Column B
Foo 5
Bar 46
Baz 100
Foo 10
... ...

I'm trying to create a stacked histogram from a pandas DataFrame that stacks the series obtained by grouping by Column A the values of Column B.

I used pd.hist(by='Column A', column='Column B', stacked=True), but, as the docs say, 'by' will create an histogram by each unique value in the column specified.

Any hints?

Upvotes: 1

Views: 2056

Answers (2)

Priya
Priya

Reputation: 743

You can do this way:

# taking count since histogram anyway represents the frequencies in each bin.
grp = df.groupby("col_A").agg("count")
plt.hist([grp.index, grp.values.reshape(3,)], bins=3, range=(1,6), stacked=True, color = ['r','g'])
plt.show()

Upvotes: 0

not_speshal
not_speshal

Reputation: 23146

IIUC, you want this:

df.pivot(columns="Column A")["Column B"].plot(kind="hist", stacked=True)

Upvotes: 4

Related Questions