Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (18.4k points)

I have a dataset that contains a column named class which is a binary data type that has 1 and 0. I am trying to extract the values 1 from the class column using the subset. This is the code which I am using:

fraud_indices = np.array(dataset[dataset.Class == 1].index)

fraud_samples = dataset.iloc[fraud_indices, 

I am getting the error:

"positional indexers are out-of-bounds"

I don't understand why am I getting this out of bound error when I am using a single column. Can anyone help me solve it?

1 Answer

0 votes
by (36.8k points)

As per my knowledge, you need to use boolean indexing  

fraud_samples = dataset[dataset.Class == 1]

if you want to use indices 

fraud_indices = fraud_samples.index

but I am getting an error:

positional indexers are out-of-bounds

I am getting this error because we have not specified the indices. I am using the sample dataset as shown below:

dataset = pd.DataFrame({'Class':[0,1,0,1]}, index=[0,1,3,5])

print (dataset)

   Class

0      0

1      1

3      0

5      1

fraud_indices = np.array(dataset[dataset.Class == 1].index)

print (fraud_indices)

[1 5]

You cannot select the rows it thoughts you  an error:

fraud_samples = dataset.iloc[fraud_indices, 

print (fraud_samples)

Error:

IndexError: positional indexers are out-of-bounds

But you can select using the indices numbers:

fraud_samples = dataset.loc[fraud_indices, 

print (fraud_samples)

   Class

1      1

5      1

As shown above, you will get the proper output. If you are a beginner and want to know more about Data Science the do check out the Data Science course

Browse Categories

...