Home > Software design >  How to walk through two identically shaped dataframes cell by cell and apply some logic by cell comp
How to walk through two identically shaped dataframes cell by cell and apply some logic by cell comp

Time:01-27

I need to compare two identically shaped dataframes cell by cell applying some function depending on the comparison result. some psydocode for illustration

for cell in dataframes:
  if cells are equal:
    do someting
  else:
    do someting else

I checked the DataFrame.compare method but didn't get anywhere.

CodePudding user response:

If i understood your question clearly, this could help:

for i in range(df1.shape[0]):
    for j in range(df1.shape[1]):
        if df1.iloc[i, j] == df2.iloc[i, j]:
            # Do something
            pass
        else:
            # Do something else
            pass
  • Related