lundi 10 août 2020

Multiple condition in 'if' loop to compare values of column

I have 2 columns in index 12 and 13 of dataframe df_2. I want to compare 'i+1' row of 2 columns with 'i' row of the same column. Only if both row doesn't match then i want to increment the value i assign. But the code i have written is failing somewhere. What is the problem?

h_count = 0
current = "G"
status = []
for i in range(len(df_2)):
    if (i < len(df_2)-1) and ((df_2.iloc[i+1, 12] and df_2.iloc[i+1, 13]) == (df_2.iloc[i, 12] and df_2.iloc[i, 13])):
        status.append(f"G{h_count}")
    else:
        status.append(f"G{h_count}")
        h_count += 1

enter image description here

I want it to be G4 here because one of the value doesn't match above. I think it is comparing only second column.

PN: I know I can modify loop by comparing one column first and then compare second column inside that loop. but how can I do it in one condition?

Aucun commentaire:

Enregistrer un commentaire