How to Increment the cell index

Question:

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)
Asked By: Ragav

||

Answers:

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)
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.