以列名称为x轴绘制直方图

时间:2019-10-06 12:02:48

标签: python pandas histogram

我想获取一个数据框,并生成一个直方图,其列名称为x轴,计数为y轴

数据集:

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

2 个答案:

答案 0 :(得分:0)

尝试一下:

import matplotlib.pyplot as plt

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

您应该得到这样的东西:

enter image description here

答案 1 :(得分:0)

您需要:

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

然后您就可以使用 显示总和:

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

enter image description here

显示零和一的计数:

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)

输出:

      count_1  count_0
sess        2        8
lea         2        8
opps        0       10

enter image description here