I need to call pd.DataFrame by index. Extracted it by data.sample:
features = data.drop(['target'], axis=1)
target = data['target']
valid_indexes = data.sample(n=sample_size, replace=False, random_state=12345).index
train_indexes = data.drop(valid_indexes).index
The result seems to be correct:
print(valid_indexes)
print(train_indexes)
Int64Index([184, 75, 10, 101, 240, 112, 211, 238, 186, 67,
...
207, 301, 99, 175, 9, 254, 157, 255, 260, 270],
dtype='int64', length=101)
Int64Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 12,
...
286, 287, 289, 290, 292, 296, 297, 298, 299, 300],
dtype='int64', length=202)
But then when I try to call the dataframe by those Indexes, I get this:
features[[valid_indexes]]
target[[valid_indexes]]
KeyError: "None of [Index([(184, 75, 10, 101, 240, 112, 211, 238, 186, 67, 68, 156, 87, 35, 147, 20, 288, 181, 173, 222, 57, 302, 250, 110, 278, 33, 103, 115, 55, 85, 120, 40, 30, 187, 163, 50, 54, 11, 170, 242, 150, 244, 239, 183, 225, 280, 16, 73, 52, 193, 132, 294, 188, 295, 106, 141, 100, 151, 291, 47, 18, 97, 224, 174, 53, 234, 232, 128, 205, 71, 293, 88, 216, 169, 258, 134, 223, 168, 164, 272, 259, 38, 269, 70, 127, 161, 219, 233, 130, 197, 241, 207, 301, 99, 175, 9, 254, 157, 255, 260, ...)], dtype='object')] are in the [columns]"
When I try to call data by, say: features[2:14] I get correct dataframe. But my way using variable is wrong in some way. What are possible solutions?