python pandas is giving a keyerror for a column I group by, even though a boolean expression shows that the column is part of the dataframe

Viewed 767

I cannot seem to print the following line: summarydata["Name"].groupby(["Tag"]).size()

without getting the error:

  File "C:\Users\rspatel\untitled0.py", line 76, in <module>
    print(summarydata["Name"].groupby(["Tag"]).size())

  File "C:\Users\rspatel\Anaconda3\lib\site-packages\pandas\core\series.py", line 1720, in groupby
    return SeriesGroupBy(

  File "C:\Users\rspatel\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py", line 560, in __init__
    grouper, exclusions, obj = get_grouper(

  File "C:\Users\rspatel\Anaconda3\lib\site-packages\pandas\core\groupby\grouper.py", line 811, in get_grouper
    raise KeyError(gpr)

KeyError: 'Tag'

I have checked that Tag is included as a column in the summarydata dataframe by the following:

if 'Tag' in summarydata.columns:
    print("true")
else :
    print("false")

which prints out as true. Therefore I am not sure why a key error is being thrown when the column is in the dataframe.

1 Answers

You are trying to group by a key on the column itself. Instead you want:

summarydata["name"].groupby(summarydata["Tag"])

from the docs:

by: (mapping, function, label, or list of labels)

Used to determine the groups for the groupby. If by is a function, it’s called on each value of the object’s index. If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups (the Series’ values are first aligned; see .align() method). If an ndarray is passed, the values are used as-is to determine the groups. A label or list of labels may be passed to group by the columns in self. Notice that a tuple is interpreted as a (single) key.

In other words you can pass it anything ;) (This is why I don't like pandas...)

You can pass it:

  • a fn, which is called on every value
  • a dict(!) or series, which will be used to group (what you want)
  • a numpy array (ditto)
  • a label or list of labels, in which case it groups by the column in the object in question

But in your case, you've already selected the name column, so the Tag column no longer exists! (Think about what summarydata["name"] returns.)

So if you want to group like that, you need to group first:

summarydata.groupby("Tag")["name"]
Related