Home > Software design >  How to Increment the cell index
How to Increment the cell index

Time:12-26

I want to read the data (key&value) from excel and check whether the given data is present in a dictionary or not. Example for excel :

key       value
====      =====
v54      desktop
v7         nav
events    event6

When I tried the following code it gives me only the current row

OUTPUT:

Key-value pair found in dictionary events : event6

EXPECTED OUTPUT:

Key-value pair found in dictionary v54:desktop
Key-value pair found in dictionary v7:nav
Key-value pair found in dictionary events : event6
for index in range (len(df)):
   key = df.loc[index,'key']
   value = df.loc[index,'value']

if key in dict and dict[key] == value:
    print("Key-value pair"" " color.GREEN "found" color.END  " ""in dictionary"" " color.GREEN  key  " : "  value color.END)
    df.at[index, 'result'] = 'PASS'
    
else:
    print("Key-value pair"" " color.RED  "not found" color.END " " "in dictionary"" " color.RED key  " : "  value color.END)
    df.at[index, 'result'] = 'FAIL'
        

# Save the updated dataframe to the Excel file
df.to_excel('test.xlsx', index=False)

CodePudding user response:

Your indentation is wrong. it should be like this :

for index in range (len(df)):
   key = df.loc[index,'key']
   value = df.loc[index,'value']

    if key in dict and dict[key] == value:
        print("Key-value pair"" " color.GREEN "found" color.END  " ""in dictionary"" " color.GREEN  key  " : "  value color.END)
        df.at[index, 'result'] = 'PASS'

    else:
        print("Key-value pair"" " color.RED  "not found" color.END " " "in dictionary"" " color.RED key  " : "  value color.END)
        df.at[index, 'result'] = 'FAIL'


# Save the updated dataframe to the Excel file
df.to_excel('test.xlsx', index=False)
  • Related