Group By and ILOC Errors

Question:

I’m getting the following error when trying to groupby and sum by dataframe by specific columns.

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

I’ve checked other solutions and it’s not a double column name header issue.

See df3 below which I want to group by on all columns except last two, I want to sum()

dfs head shows that if I just group by the columns names it works fine but not with iloc which I know to be the correct formula to pull back column I want to group by.

I need to use ILOC as final dataframe will have many more columns.

enter image description here

Asked By: Mark Sheppard

||

Answers:

df.iloc[:,0:3] returns a dataframe. So you are trying to group dataframe with another dataframe.

But you just need a column list.

can you try this:

dfs = df3.groupby(list(df3.iloc[:,0:3].columns))['Churn_Alive_1','Churn_Alive_0'].sum()
Answered By: Clegane
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.