Emm
Emm

Reputation: 2507

Plotting a histogram with the column names as the x-axis

I would like to take a dataframe, and produce a histogram with the column names as the x-axis and the count as the y-axis

Dataset:

sess    lea opps
  0      0    0
  1      1    0
  0      0    0
  0      0    0
  0      0    0
  0      0    0
  1      1    0
  0      0    0
  0      0    0
  0      0    0

Upvotes: 1

Views: 2991

Answers (2)

ansev
ansev

Reputation: 30920

you need:

import matplotlib.pyplot as plt
%matplotlib inline #only jupyter notebooks

then you can use to show the sum:

df.sum().plot(kind='bar')

enter image description here

to show the count of zeros and ones:

count=pd.concat([df.sum().rename('count_1'),df.eq(0).sum().rename('count_0')],axis=1)
print(count)
count.plot(kind='bar',stacked=True)

Output:

      count_1  count_0
sess        2        8
lea         2        8
opps        0       10

enter image description here

Upvotes: 1

adrianp
adrianp

Reputation: 1019

Try this:

import matplotlib.pyplot as plt

counts = df.sum()
x, y = counts.index, counts.values
plt.bar(x, y)

You should get something like this:

enter image description here

Upvotes: 1

Related Questions