pandas -来自两列的直方图? [英] pandas - histogram from two columns?

查看:56
本文介绍了 pandas -来自两列的直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

data = pd.DataFrame().from_dict([r for r in response])
print data

     _id  total
0    213      1
1    194      3
2    205      156
...

现在,如果我打电话给我

Now, if I call:

data.hist()

我将获得两个独立的直方图,每列一个.这不是我想要的.我想要的是使用这两列创建的单个直方图,其中一列被解释为一个值,另一列被解释为该值的多次出现.我该怎么做才能生成这样的直方图?

I will get two separate histograms, one for each column. This is not what I want. What I want is a single histogram made using those two columns, where one column is interpreted as a value and another one as a number of occurrences of this value. What should I do to generate such a histogram?

我尝试过:

data.hist(column="_id", by="total")

但这会生成带有错误消息的更多(空)直方图.

But this generates even more (empty) histograms with error message.

推荐答案

您始终可以转到较低级别的

You can always drop to the lower-level matplotlib.hist:

from matplotlib.pyplot import hist
df = pd.DataFrame({
    '_id': np.random.randn(100),
    'total': 100 * np.random.rand()
})
hist(df._id, weights=df.total)

这篇关于 pandas -来自两列的直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆