绘制以列名作为 x 轴的直方图

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

我想获取一个数据框,并生成一个直方图,其中列名作为 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

试试这个:

import matplotlib.pyplot as plt

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

你应该得到这样的东西:

你需要:

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

那么你可以使用 显示总和:

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

显示 0 和 1 的数量:

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