Question

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:

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

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

Était-ce utile?

La solution

Maybe try to use the .values attribute (this returns the data as a numpy array), so:

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

I assume the reason this does not work is because the matplotlib hist method 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 0 anymore)


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

df[df.TYPE=='SU4'].GVW.hist(bins=50)

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top