Home > front end >  Count cells in for loop
Count cells in for loop

Time:09-13

I am working on my Python project and I'm stuck on a for loop. Basically, I need to conditionally count cells in a column (bank_account) where the cells are populated, omitting empty cells and cells starting with '00000'.

Here's part of the code:

sheets = vendor_list.sheetnames
NIP_col = locate_col(vendor_list, sheets[0], 'VTID05')
bank_account_col = locate_col(vendor_list, sheets[0], 'BKAC05')
vendor_list_data_dict = {"VTID05":[],"BKAC05":[]}

for y in range(100000):
    bank_account_item_ID = str(vendor_list['Sheet1'].cell(row=2 y, column=bank_account_col).value)
    if bank_account_item_ID != "None" and str(bank_account_item_ID).startswith('00000'):
        vendor_list_data_dict["BKAC05"].append(
               str(vendor_list['Sheet1'].cell(
                   row=2 y, column=bank_account_col).value).replace(" ",""))
    else:
        pass

Result I get is number of cells starting with '00000'. Why? How do I fix this to count only populated cells with bank account numbers, without empty cells and cells starting with '00000'?

CodePudding user response:

Your comparison is flawed. It should read:

if bank_account_item_ID != "None" and not str(bank_account_item_ID).startswith('00000'):

because you apparently want to ignore those cells.

You may also consider to invert the condition, siche this alliws to drop the else branch completely.

CodePudding user response:

This is because your condition

bank_account_item_ID != "None" and str(bank_account_item_ID).startswith('00000')

attempts to find those cells that start with '00000' and at the same time don't equal the string that is composed of the letters 'N', 'o', 'n', and 'e'. If stick to this code, I would recommend to replace this line with

not (np.isnan(bank_account_item_ID) or str(bank_account_item_ID).startswith('00000'))
  • Related