How to exclude a list from a list?

Viewed 84

I have a data frame consisting of 4 columns and 30000 rows. The last column is my target. I needed to exclude a sublist from that based on indices, and make a new dataset. To shed more light on it, I needed to exclude from the main dataset, 100 indices before and 100 indices after the index. I tried to define a function to do that through a for loop like below:

import pandas as pd

from sklearn.datasets import make_classification
df = make_classification(n_samples=10000, n_features=4)

NEW_middle_index = [1037, 1112, 1187, 1262, 1337, 1412, 1562]

XX , yy = [],[]
def data_reader(df,NEW_middle_index):
  for i in NEW_middle_index:
    
    y = df.iloc[i-100:i+100:,3]
    X = df.iloc[i-100:i+100,:].drop(columns='Target')
    XX.append(X)
    yy.append(y)
  return XX,yy

data_reader(df,NEW_middle_index) #edited here

It could do it for XX correctly but for yy, it is not capable of excluding and it repeats the same number of the first group which is incorrect. Also, I used a dictionary to do that, and again I got the same answer.

1 Answers

I have found My solution to exclude a list from a list using enumerate inside of for-loop.

The only necessary thing was using enumerate. The code below is the final solution of that.

for extra_index, i in enumerate(NEW_middle_index):

      
    y = df1.iloc[(len(new_df)*extra_index)+i-100:(len(new_df)*extra_index)+i+100,2]
    
    X = df1.iloc[i-100:i+100,:].drop(columns='Target')

with this method, in every cycle of for-loop it opens the file which relates to that cycle in exclude a required list from that file and it closes the file and so on.

Related