Search code examples
pythonpandasdataframescikit-learnkeyerror

How do I prevent KeyErrors in python?


I am having KeyError problems when working on a MinMaxScaler in a machine learning project. This is my relevant code:

df = pd.read_csv(io.BytesIO(uploaded['Root_Work_Sample.csv']))
print(df.shape)
print(df.columns)
display(df.head(5))
print(df.dtypes)
train_cols = ["feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11, feature12, feature13, feature14, y"]
df_train, df_test = train_test_split(df, train_size=1000, test_size=876, shuffle=False)
print("Train--Test size", len(df_train), len(df_test))
print(df_train)
print(df_test)
 
# scale the feature MinMax, build array
x = df_train.loc[:,train_cols].values  #THE ERROR IS ON THIS LINE
min_max_scaler = MinMaxScaler()
x_train = min_max_scaler.fit_transform(x)
x_test = min_max_scaler.transform(df_test.loc[:,train_cols])

This is the error I get:

KeyError: "None of [Index(['feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11, feature12, feature13, feature14, y'], dtype='object')] are in the [columns]"

Is there any suggestions on how to fix this and general practice on how a novice like me can avoid these sort of errors?


Solution

  • df_train is not a dataframe, it's a 2D numpy array, so you can't use loc method on it. I guess you are using train_test_split function in a wrong way. And also you are specifying train_cols wrongly, you should wrap each feature in a quotation mark like this:

    train_cols = ["feature", "feature2",....]
    

    Try this:

    X, y = df[train_cols], df["y"]
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=876, shuffle=False)
    
    scaler = MinMaxScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)