Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dataframe KeyError – not in index

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading