dataframe index groupby error ValueError: 'GL' is both an index level and a column label

Viewed 1292

I have this code which runs well until this morning:

# delete rows of 2019
        df.drop(df[df.month.str.contains('2019')].index, inplace=True)

        df.sort_values(by=['GL',  'month'], inplace=True)
        df["diffDebit"] = df.groupby('GL')['GL_Debit'].diff().fillna(df['GL_Debit'])
        df["diffCredit"] = df.groupby('GL')['GL_Credit'].diff().fillna(df['GL_Credit'])

error is : ValueError: 'GL' is both an index level and a column label, which is ambiguous.

If I delete df.drop(df[df.month.str.contains('2019')].index, inplace=True)

It works again, but I need to delete these rows before. Any idea?

Template of dataframe: enter image description here

1 Answers

Find a solution:

in fact just add [] for ['GL'] in groupby as follows:

df["diffDebit"] = df.groupby(['GL'])['GL_Debit'].diff().fillna(df['GL_Debit'])
Related