问题描述
在制定如何从熊猫数据框中的分组数据绘制直方图块方面,我需要一些指导.这是一个示例来说明我的问题:
I need some guidance in working out how to plot a block of histograms from grouped data in a pandas dataframe. Here's an example to illustrate my question:
from pandas import DataFrame
import numpy as np
x = ['A']*300 + ['B']*400 + ['C']*300
y = np.random.randn(1000)
df = DataFrame({'Letter':x, 'N':y})
grouped = df.groupby('Letter')
由于无知,我尝试了以下代码命令:
In my ignorance I tried this code command:
df.groupby('Letter').hist()
失败,并显示错误消息"TypeError:无法连接'str'和'float'对象"
which failed with the error message "TypeError: cannot concatenate 'str' and 'float' objects"
最感谢您的帮助.
推荐答案
我正在努力,只是找到了一种更简单的方法来使用hist方法中的 by 关键字:
I'm on a roll, just found an even simpler way to do it using the by keyword in the hist method:
df['N'].hist(by=df['Letter'])
这是一个非常方便的快捷方式,可以快速扫描您的分组数据!
That's a very handy little shortcut for quickly scanning your grouped data!
对于将来的访问者,此通话的结果如下图:
For future visitors, the product of this call is the following chart:
这篇关于从pandas DataFrame中的分组数据绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!