【问题标题】:Plotting a histogram with the column names as the x-axis绘制以列名作为 x 轴的直方图
【发布时间】:2019-10-06 12:02:48
【问题描述】:

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

【问题讨论】:

  • sess 是索引吗?

标签: python pandas histogram


【解决方案1】:

你需要:

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

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

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

显示零和一的计数:

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

【讨论】:

    【解决方案2】:

    试试这个:

    import matplotlib.pyplot as plt
    
    counts = df.sum()
    x, y = counts.index, counts.values
    plt.bar(x, y)
    

    你应该得到这样的东西:

    【讨论】:

    • 您可以从DataFrame中显示图形,不需要创建额外的变量。检查我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2015-01-07
    • 2019-03-05
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多