user2808117
user2808117

Reputation: 4877

how to generate histogram in pandas with x-axis labels from column?

Given the following dataframe:

import pandas as pd
df = pd.read_json('{"genre":{"0":"Drama","1":"Comedy","2":"Action","3":"Thriller"},"count":{"0":1603,"1":1200,"2":503,"3":492}}')

Is there a pandas one-liner/fast way to generate a histogram where bars are based on the "count" column, and x-axis labels are the "genre" column?

Upvotes: 2

Views: 8165

Answers (1)

Ffisegydd
Ffisegydd

Reputation: 53688

Yes you can select the columns to be used with the x and y keyword arguments. You also select the kind of plot you want, in this case a hist, using kind='bar'.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_json('{"genre":{"0":"Drama","1":"Comedy","2":"Action","3":"Thriller"},"count":{"0":1603,"1":1200,"2":503,"3":492}}')

df.plot(x='genre', y='count', kind='bar', legend=False)

plt.show()

Note that I've also used legend=False to remove the legend, you could leave this in if you so wish.

enter image description here

Upvotes: 10

Related Questions