ddd
ddd

Reputation: 5029

How to plot histogram of mean of one column while bins are defined by another column in Pandas

I want to plot a histogram for two columns of my Pandas DataFrame.

Bins are defined on a ratio column, e.g. [0-0.1, 0.1-0.2,...,0.9-1.0], and I want to plot the mean value of another column called feet for each bin.

Is there a way to just plot the histogram without generating a new column ?

Upvotes: 2

Views: 2183

Answers (1)

Stef
Stef

Reputation: 30589

You don't need to create a new column, just pass a function to groupby:

Example:

import pandas as pd
import numpy as np
df = pd.DataFrame({'ratio':np.random.rand(100), 'feet': np.random.rand(100)*10})
df.groupby(pd.cut(df.ratio, np.linspace(0,1,11))).feet.mean().plot.bar()

enter image description here PS: Starting with version 1.1.0 of pandas you can directly specify the y label like ...plot.bar(ylabel='Mean feet').

Upvotes: 4

Related Questions