vendredi 20 juillet 2018

Loop being executed but particular code being ignored in python?

TASK: To remove a set list of serial numbers amongst 1000+ from a csv. Once a button on the GUI is clicked this body of code should execute:

serialNums = ["123", "456", "789", "045"]

  count = 0

  for index, row in df.iterrows():
    try:
      serialNum = row [80]
    except:
      serialNum = 0
    for cnum in range(len(serialNums)):
      if(serialNum == serialNums[cnum]):
        df = df.drop(df.index[index])
        count = count + 1
        print("Serial number found. " + row[3] + " has been removed." )
        output("Number of people removed: " + str(count))


  if count == 0:
    output("Serial not found!")

Works well, it recognises the number of matching serial numbers and increments the count. However, the df = df.drop(df.index[index]) only works when I click the button for the number of serial numbers it recognises in the file. E.g. if it finds 2 serial numbers in that file, I must click the button twice for it to remove those serial numbers. So it's a matter of convenience to click the button once and remove all serials in one click. I've tried continue and I don't think break is appropriate in this case.

Aucun commentaire:

Enregistrer un commentaire