ValueError: Grouper for <something> not 1-dimensional ValueError: Grouper for <something> not 1-dimensional pandas pandas

ValueError: Grouper for <something> not 1-dimensional


Simplified problem

I also ran into this problem, and found the cause of it and the obvious solution

To recreate this:

df = pd.DataFrame({"foo": [1,2,3], "bar": [1,2,3]})df.rename(columns={'foo': 'bar'}, inplace=True)   bar  bar0    1    11    2    22    3    3df.groupby('bar')ValueError: Grouper for 'bar' not 1-dimensional

Just like a lot of cryptic pandas errors, this one too stems from having two columns with the same name.

Figure out which one you want to use, rename or drop the other column and redo the operation.

Solution

Rename the columns like this

df.columns = ['foo', 'bar']   foo  bar0    1    11    2    22    3    3df.groupby('bar')<pandas.core.groupby.DataFrameGroupBy object at 0x1066dd950>


TL;DR:
Quick example: if I am to groupby a bunch of people by careers, you are either an eng or a tech, can't be both, otherwise groupby won't know if to put you in tech group or eng group.
Your code, unfortunately assigned some people (some indexes in you df) into eng AND tech at the same time.

First of all, just to make sure we TRULY understand what groupby() does.

We will be using this example fruit df thru out:

import pandas as pdimport numpy as npdf = pd.DataFrame(    {"fruit": ['apple', 'apple', 'orange', 'orange'], "color": ['r', 'g', 'b', 'r']},    index=[11, 22, 33, 44],)"""[df] df:+----+---------+---------+|    | fruit   | color   ||----+---------+---------|| 11 | apple   | r       || 22 | apple   | g       || 33 | orange  | b       || 44 | orange  | r       |+----+---------+---------+"""

Below is a very valid df.groupby(), not using any column names:

gp = df.groupby(    {        0: 'mine',        1: 'mine',        11: 'mine',        22: 'mine',        33: 'mine',        44: 'you are rats with wings!',    })"""[df] [group] mine:+----+---------+---------+|    | fruit   | color   ||----+---------+---------|| 11 | apple   | r       || 22 | apple   | g       || 33 | orange  | b       |+----+---------+---------+[df] [group] you are rats with wings!:+----+---------+---------+|    | fruit   | color   ||----+---------+---------|| 44 | orange  | r       |+----+---------+---------+"""

Wait, the groupby() didn't even use 'fruit' or 'color' at all?!
That's right! groupby() doesn't need to care about df or 'fruit' or 'color' or Nemo, groupby() only cares about one thing, a lookup table that tells it which index is mapped to which label (ie. group).

In this case, for example, the dictionary passed to the groupby() is instructing the groupby() to:
if you see index 11, then it is a "mine", put the row with that index in the group named "mine".
if you see index 22, then it is a "mine", put the row with that index in the group named "mine".
...
even 0 and 1 not being in df.index is not a problem

Conventional df.groupby('fruit') or df.groupby(df['fruit']) follows exactly the rule above. The column df['fruit'] is used as a lookup table, it tells groupby() that index 11 is an "apple"

Now, regarding: Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional

what it is saying is really: for some or all indexes in df, you are assigning MORE THAN just one label

Let's examine some possible errors using the above example:
[x] df.groupby(df) will not work, you gave groupby() a 2D mapping, each index was given 2 group names. It will complain: is index 11 an "apple" or an "r"? make up your mind!

[x] the below codes will also not work. Although the mapping is now 1D, it is mapping index 11 to "mine" as well as "yours". Pandas' df and sr allow none-unique index, so be careful.

mapping = pd.DataFrame(index= [ 11,     11,      22,     33,     44    ],                        data = ['mine', 'yours', 'mine', 'mine', 'yours'], )df.groupby(mapping)# different error message, but same ideamapping = pd.Series(   index= [ 11,     11,      22,     33,     44    ],                        data = ['mine', 'yours', 'mine', 'mine', 'yours'], )df.groupby(mapping)


Happened to me when I accidentally created MultiIndex columns:

>>> values = np.asarray([[1, 1], [2, 2], [3, 3]])# notice accidental double brackets around column list>>> df = pd.DataFrame(values, columns=[["foo", "bar"]])# prints very innocently>>> df  foo bar0   1   11   2   22   3   3# but throws this error>>> df.groupby("foo")ValueError: Grouper for 'foo' not 1-dimensional# cause:>>> df.columnsMultiIndex(levels=[['bar', 'foo']],           labels=[[1, 0]])# fix by using correct columns list>>> df = pd.DataFrame(values, columns=["foo", "bar"])>>> df.groupby("foo")<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x7f9a280cbb70>