anon_swe
anon_swe

Reputation: 9355

Turning Pandas DataFrame into Histogram Using Matplotlib

I have a Pandas DataFrame which has a two columns, pageviews and type:

    pageviews   type
0   48.0        original
1   1.0         licensed
2   181.0       licensed
...

I'm trying to create a histogram each for original and licensed. Each histogram would (ideally) chart the number of occurrences in a given range for that particular type. So the x-axis would be a range of pageviews and the y-axis would be the number of pageviews that fall within that range.

Any recs on how to do this? I feel like it should be straightforward...

Thanks!

Upvotes: 0

Views: 512

Answers (1)

jack6e
jack6e

Reputation: 1522

Using your current dataframe: df.hist(by='type')

For example:

# Me recreating your dataframe
pageviews = np.random.randint(200, size=100)
types = np.random.choice(['original','licensed'], size=100)

df = pd.DataFrame({'pageviews': pageviews,'type':types})

# Code you need to create faceted histogram by type
df.hist(by='type')

pandas.DataFrame.hist documentation

Upvotes: 4

Related Questions