0

In my code below I am trying to randomly parse a dataframe. Then based on the random number generated I want to delete/drop that row and but it into another dataframe. Later I want then do a comparison between the plucked row and the entire dataframe entries left. However, I cannot seem to remove the randomly generated index row from my dataframe. Whenever I run my print statement after it still appears. Any idea what I am doing wrong?

I have tried data.drop(data.index[y]) but this does not drop an rows as my dataframe when printed at the end shows all of the rows and looks like nothing was dropped.

col_names = ['letter','m00', 'mu02', 'mu11', 'mu20', 'mu03', 'mu12', 'mu21', 'mu30']
data.head()
data = pd.read_csv('traindat.txt', delim_whitespace = True,skiprows = 1, header = None)
plucked_df = pd.DataFrame(columns = col_names)
data.columns = col_names
#df.loc[row,column] or df.loc[0,'mu00']
#print(data.loc[3,'m00'])
print(data)
x = len(data)
#df_marks = df_marks.append(new_row, ignore_index=True)
for i in range(0,x):
    y = random.randint(0,x)
    #print(y)
    print(data.loc[y])
    plucked_df = plucked_df.append(data.loc[y],ignore_index =True)
    #print(plucked_df)
    #data.drop([y])
    data.drop(data.index[y])
    print(data.index[y])
    #del data[y]
    data.reset_index(drop=True,inplace = True)
    x = len(data) -1
    #print(data)
#print(len(data))
print("data",data)
print("pluck",plucked_df)
tarkan
  • 69
  • 1
  • 8
  • 2
    Try running this data = data.drop( data.index[y] ) instead of data.drop(data.index[y]) – kgkmeekg Feb 09 '20 at 19:38
  • Use `inplace=True` in the `drop()` call. Refer: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html – amanb Feb 09 '20 at 19:39
  • 1
    @amanb Actually, you should prefer re-assignment over `inplace=True`. See https://stackoverflow.com/a/60020384/4909087 – cs95 Feb 09 '20 at 20:09
  • @cs95, thanks for pointing this out.. interesting to know, I've been using it almost wherever possible for succinctness but will think twice now as it may be deprecated in future – amanb Feb 09 '20 at 20:13

0 Answers0