ValueError: Grouper for not 1-dimensional

前端 未结 5 1861
梦如初夏
梦如初夏 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 19:53

    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  bar
    0    1    1
    1    2    2
    2    3    3
    
    df.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  bar
    0    1    1
    1    2    2
    2    3    3
    
    df.groupby('bar')
    
    

提交回复
热议问题