Dataframe KeyError – not in index

Question:

I’m struggling to copy the below stated 3 columns from the non_contras_by_account dataframe columns. Whenever I run this code I get a KeyError that ‘account key’ not in index. This is odd because ‘account_key’ is clearly present in this dataframe.

Any help on this would be much appreciated!

non_contras_by_account = non_contras.groupby('account_key').sum()

non_contra_tab_df = non_contras_by_account[['account_key',value_col,'abs_val']]

KeyError: "[‘account_key’] not in index"

Asked By: mot375

||

Answers:

Use as_index=False in the first command to keep groupby from moving it as index:

non_contras_by_account = non_contras.groupby('account_key', as_index=False).sum()
Answered By: mozway
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.