pandas 过滤数据框的熊猫直方图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/22492042/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Histogram of Filtered Dataframe
提问by marillion
This has been driving me mad for the one last hour. I can draw a histogram when I use:
这让我发疯了最后一个小时。我可以在使用时绘制直方图:
hist(df.GVW, bins=50, range=(0,200))
I use the following when I need to filter the dataframe for a given condition in one of the columns, for example:
当我需要为其中一列中的给定条件过滤数据框时,我使用以下内容,例如:
df[df.TYPE=='SU4']
So far, everything works. When I try to get a histogram of this filtered data I get a key error: KeyError: 0L. I use the following for the histogram of filtered data:
到目前为止,一切正常。当我尝试获取此过滤数据的直方图时,我收到一个关键错误:KeyError: 0L. 我将以下内容用于过滤数据的直方图:
hist(df[df.TYPE=='SU4'].GVW, bins=50, range=(0,200))
Is there a syntax error somewhere? Thanks for the help!
某处是否存在语法错误?谢谢您的帮助!
回答by joris
Maybe try to use the .valuesattribute (this returns the data as a numpy array), so:
也许尝试使用该.values属性(这将数据作为 numpy 数组返回),因此:
hist(df[df.TYPE=='SU4'].GVW.values, bins=50, range=(0,200))
I assume the reason this does not work is because the matplotlib histmethod tries to access the first 0-index element of the input. But because the Series uses its integer index as label and not location, this gives a key error for a sliced Series (as the first element will not have index 0anymore)
我认为这不起作用的原因是 matplotlibhist方法试图访问0输入的第一个-index 元素。但是因为系列使用其整数索引作为标签而不是位置,这给切片系列带来了关键错误(因为第一个元素将不再有索引0)
And indeed, as @AndyHayden says, you can also use the pandas histmethod:
事实上,正如@AndyHayden 所说,你也可以使用 pandashist方法:
df[df.TYPE=='SU4'].GVW.hist(bins=50)
回答by user3685329
I had a similar issue plotting a dataframe I derived using a query. I found that if after deriving the frame I used the reset_index() function on the derived frame it resolved the issue.
我在绘制使用查询导出的数据框时遇到了类似的问题。我发现如果在派生框架后我在派生框架上使用了 reset_index() 函数,它就解决了这个问题。

