How To Solve KeyError: u”None of [Index([..], dtype=’object’)] are in the [columns]”

The problem is that there are spaces in your column names; here is what I get when I save your data and load the dataframe as you have done:

# result:
Index(['LABEL', ' F1', ' F2', ' F3', ' F4', ' F5', ' X', ' Y', ' Z', ' C1',
       ' C2'],

so, putting back these spaces in the column names eliminates the error:

train_features = train[[' F1',' F2',' F3',' F4',' F5',' X',' Y',' Z',' C1',' C2']] # works OK

But arguably, having spaces in your column names is not good practice (you saw what can happen!); so it is better to eliminate them during loading. Here is the end to end code to do that (eliminating also the unnecessary second dataframe):

import pandas as pd
df= pd.read_csv("lettera.csv", delimiter=",", header=None, skiprows=1, names=['LABEL','F1','F2','F3','F4','F5','X','Y','Z','C1','C2'])

from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size = 0.2)
train_features = train[['F1','F2','F3','F4','F5','X','Y','Z','C1','C2']] # works OK

Leave a Comment