Home > Enterprise >  data frame slicing several problems
data frame slicing several problems

Time:02-11

I don't understand slicing anymore!! I am doing strat_shuf_split.split and separate my data into training and test sets. I run this:

X_train = data1.loc[train_idx, cols]
y_train = data1.loc[train_idx, 'nClass']
X_test  = data1.loc[test_idx, cols]
y_test  = data1.loc[test_idx, 'nClass']

and i get KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported. I run it on a year old notebook and it runs fine. Why? I try this:

X_train = X.loc[X.index.intersection(train_idx),:]
y_train = y.loc[y.index.intersection(train_idx),:]
X_test = X.loc[X.index.intersection(test_idx),:]
y_test = y.loc[y.index.intersection(test_idx),:]

and I get error on y_train that there are too many indexers. Don't know what to do.

CodePudding user response:

You can try the following format:

X_train = data1[cols].iloc[train_idx]
y_train = data1['nClass'].iloc[train_idx]
X_test  = data1[cols].iloc[test_idx]
y_test  = data1['nClass'].iloc[test_idx]
  • Related