Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe Key Error Column not in index

Tags:

pandas

I am reading an excel file into pandas using pd.ExcelFile. It reads correctly and I can print the dataframe. But when I try to select a subset of columns like:

subdf= origdf[['CUTOMER_ID','ASSET_BAL']]

I get error:

 KeyError: "['CUTOMER_ID' 'ASSET_BAL'] not in index"

Do I need to define some kind of index here? When I printed the df, I verified that the columns are there.

like image 781
Victor Avatar asked Jul 11 '18 14:07

Victor


1 Answers

Ensure that the columns actually exist in the dataframe. For example, you have written CUTOMER and not CUSTOMER, which I assume is the correct name.

You can verify the column names by using list(origdf.columns.values).

like image 184
SikZone Avatar answered Sep 22 '22 09:09

SikZone