ValueError: Grouper for not 1-dimensional

前端 未结 5 1843
梦如初夏
梦如初夏 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:09

    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 bar
    0   1   1
    1   2   2
    2   3   3
    
    # but throws this error
    >>> df.groupby("foo")
    ValueError: Grouper for 'foo' not 1-dimensional
    
    # cause:
    >>> df.columns
    MultiIndex(levels=[['bar', 'foo']],
               labels=[[1, 0]])
    
    # fix by using correct columns list
    >>> df = pd.DataFrame(values, columns=["foo", "bar"])
    >>> df.groupby("foo")
    
    

提交回复
热议问题