Skip to content
Advertisement

For doesn’t restart on dataframe in python

i need do read the rows of a dataframe but it seems to stop at the first row. I also tried with iterrows but the results are similar.

for i in range(1, 100):
        #for i, r in d.iterrows():
          if self.total_count >= 100:
              done = True
          else :
              done = False
          self.total_count += 1
          h = d.loc[i]
          print(h)
          if action == 0: 
              for index, row in df.iterrows():
                  if h['id'] == row['id']:
                      reward = 1
                      print("Equals")
                      
                  else:
                      reward = 0
                      self.miss_count += 1
                      
          elif action == 1: 
              num = random.randrange(1, len(df), 1)
              df = df.drop(df.index[[num]])
              df = df.append(h, ignore_index=True)
              reward = 0
              print(df)
          info = {}
          #M = self.df['id'].to_numpy()
          return df, reward, done, info

and the outpus is :

id      4z_3v0atqk0
size            131
Name: 1, dtype: object
Equals
id      4z_3v0atqk0
size            131
Name: 1, dtype: object
Equals

so the for doesn’t iterate. I hope someone can help me, thank you so much.

Advertisement

Answer

You are performing

return df, reward, done, info

inside the loop.
This breaks the loop on the very first iteration

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement