Search code examples
pythonpandashistogram

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?


Solution

  • IIUC, you want this:

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