Pandas Bar Graph Filtered Frame

It drove me crazy in the 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 is working. When I try to get a histogram of this filtered data, I get a key error: KeyError: 0L . For a histogram of filtered data, I use the following:

 hist(df[df.TYPE=='SU4'].GVW, bins=50, range=(0,200)) 

Is there a syntax error somewhere? Thanks for the help!

+7
python matplotlib pandas histogram
source share
2 answers

Perhaps try using the .values attribute (this returns the data as a numpy array), therefore:

 hist(df[df.TYPE=='SU4'].GVW.values, bins=50, range=(0,200)) 

I assume that the reason for this does not work, because the matplotlib hist method tries to access the first 0 index input element. But since the series uses its integer index as a label, not a location, this gives a key error for the cut series (since the first element will no longer have index 0 )


And indeed, as @AndyHayden says, you can also use the pandas hist method:

 df[df.TYPE=='SU4'].GVW.hist(bins=50) 
+10
source share

I had a similar problem with building a data core obtained using a query. I found that if after receiving the frame I used the reset_index () function in the derived frame, it solved the problem.

+4
source share

All Articles