'If statement doesn't work even if conditions are met

I'm writing some code to automate data entry at work. My issue is that the if statements are not working despite the conditions being met. I've tested the code on two excel files with the exact same data and the statements work with no issues on one file but not the other. There are no errors that appear when run with the file that doesn't work. All other commands have no issues, only the if statements are causing problems.

The only difference I can find with the two files is that one that works is unencrypted and the one that doesn't work is encrypted. I've tested out by unencrypting the file and trying it again, but it still does not work.

Here is a summarized version of what my code looks like:

import pandas as pd
import pyautogui as pya
import xlwings as xw

# Path for encrypted file

PATH = 'D:\filename.xlsx'

wb = xw.Book(PATH, password=pword)
sheet = wb.sheets['sheetname']
df = sheet.used_range.options(pd.DataFrame, index=False, header=True).value
df.head()


if (df['columnname'][row] == healthnum and df['columnname2'][row] == 5):
    pya.hotkey ('5')
elif df['columnname'][row] == healthnum and df['columnname2'][row] == 4.4:
     pya.hotkey ('4')
elif df['columnname'][row] == healthnum and df['columnname2'][row] == 3.4:
     pya.hotkey ('3')
elif df['columnname'][row] == healthnum and df['columnname2'][row] == 2:
     pya.hotkey ('2')
elif df['columnname'][row] == healthnum and df['columnname2'][row] == 1:
     pya.hotkey ('1')
        
pya.hotkey('tab')

What's supposed to happen is that if the conditions are met, then the keyboard would input '4' and then tab to the next question. What happens instead is it tabs through all the questions without putting in the data.

EDIT: I've solved this issue by checking the data types. In one file, the data was imported as a string and in the other file (the one with no issues) the data was imported as integers. So I've converted the columns to integer and everything is going smoothly now. Thanks for everyone's help



Solution 1:[1]

I don't have any knowledge about Panda, but I think you should remove the whitespaces

pya.hotkey ('5')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PhoenixHeron