ValueError: Grouper for not 1-dimensional

前端 未结 5 1870
梦如初夏
梦如初夏 2020-12-30 19:40

I\'m have the following code which creates a table and a barplot via seaborn.

#Building a dataframe grouped by the # of Engagement Types
sales_type = sales.g         


        
5条回答
  •  感动是毒
    2020-12-30 20:03

    TL;DR: what it is saying is really: for some or all indexes in df, you are assigning MORE THAN just one label, groupby() doesn't know which label it should use for grouping.

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

    We will be using this example df thru out:

    import pandas as pd
    import numpy as np
    df = 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       |
    +----+---------+---------+
    """
    

    Here is a valid df.groupby():

    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       |
    +----+---------+---------+
    """
    

    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 df.index is mapped to which label (ie. group name).

    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. df['fruit'] is the lookup table, it tells groupby() that index 11 is an "apple"

    Now, regarding: Grouper for '' not 1-dimensional

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

    [1] df.groupby(df) in this example will not work, groupby() will complain: is index 11 an "apple" or an "r"? make up your mind!

    [2] the below will also not work, although the mapping is 1D, it is mapping index 11 to mine as well as yours. 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 idea
    mapping = pd.Series(   index= [ 11,     11,      22,     33,     44    ], 
                           data = ['mine', 'yours', 'mine', 'mine', 'yours'], )
    df.groupby(mapping)
    

提交回复
热议问题