for each observation in the training set, as test sample

initialize a dataframe to save all the test run results
df_loocv = pd.DataFrame()
for index, row in df_iris.iterrows():
# save the current in the processed df_loocv
df_loocv = df_loocv.append(df_iris.iloc[index: index+1])
# drop the current row chosen as test sample
# save it in temp df
df_iris_trim = df_iris.drop(index)

test_sepal_length = float(df_iris.loc[index, ['sepal_length']])
test_sepal_width  = float(df_iris.loc[index, ['sepal_width']])
test_petal_length = float(df_iris.loc[index, ['petal_length']])
test_petal_width  = float(df_iris.loc[index, ['petal_width']])

# for each row in the dataframe, calculate the distance
for index1, row1 in df_iris_trim.iterrows():
    eucDist = sqrt(((test_sepal_length - float(row1['sepal_length'])) ** 2 +
                    (test_sepal_width - float(row1['sepal_width'])) ** 2 +
                    (test_petal_length - float(row1['petal_length'])) ** 2 +
                    (test_petal_width - float(row1['petal_width'])) ** 2))

    df_iris_trim.loc[index1, 'distance'] = eucDist
    
# sort on distance, ascending.
df_iris_trim.sort_values('distance', ascending=True, inplace=True)

# select the first K rows, into a new df
K = int(K1)
df_iris_trim_K = df_iris_trim.iloc[0:K, :]

# The resulting object will be in descending order so that the first element 
# is the most frequently-occurring element. Excludes NA values by default.
df_iris_trim_K_grouped = df_iris_trim_K['class'].value_counts()

# get the first index of the resulting pandas series above (value_counts)
pred_class = df_iris_trim_K_grouped.index[0]

# save the predicated class in the test data frame
df_loocv.loc[index, 'pred_class'] = pred_class

I AM GETTING ERROR  THINS ONE FOR SEPAL_LENGTH, SEPAL_WIDTH
KeyError: “None of [Index([‘sepal_length’], dtype=‘object’)] are in the [index]”