How do I plot a graph using Pandas?
I have a pandas dataframe like this
a b c d e f label
1 3 4 5 6 7 1
2 2 5 7 5 7 0
4 7 9 0 8 7 1
6 9 4 7 3 8 1
7 0 9 8 7 6 0
I need a histogram that looks something like this:
I tried to use the hist () function from pandas, but I cannot figure out how to include the label in the histogram to get the following graph similar to the one in the image.
source to share
It seems to me you need pivot
with counting cumcount
and last call DataFrame.plot.bar
:
df = pd.pivot(index=df.groupby('label').cumcount(), columns=df.label, values=df.a).fillna(0)
print (df)
label 0 1
0 2.0 1.0
1 7.0 4.0
2 0.0 6.0
df.plot.bar()
Or you may need an aggregate size
with a reshaping unstack
:
df = df.groupby(['label', 'a']).size().unstack(0, fill_value=0)
df.plot.bar()
Using piRSquared
data for a better sample:
source to share
Try
df.groupby('label').a.value_counts().unstack(0, fill_value=0).plot.bar()
Consider a data block df
np.random.seed([3,1415])
df = pd.DataFrame(
np.random.randint(10, size=(50, 6)),
columns=list('abcdef')
).assign(label=np.random.randint(2, size=50))
print(df.head())
a b c d e f label
0 0 2 7 3 8 7 0
1 0 6 8 6 0 2 0
2 0 4 9 7 3 2 0
3 4 3 3 6 7 7 0
4 4 5 3 7 5 9 1
demo
df.groupby('label').a.value_counts().unstack(0, fill_value=0).plot.bar()
source to share