Q:

Updating row values in a dataframe by finding similar rows based on a defined number of similar column values

I am trying to update rows in my dataframe to account for missing data by using a similarity threshold to compare how many values are the same in different rows. Below is what I am trying, but it is not updating the rows despite calling out the correct rows to fill. The current threshold is over half of the values being the same, so in this example it is any row that has 3 or more similar values, and I am looking for it to only return values that exist within the dataframe already.
threshold = .5
for index1, row1 in df.iterrows():
    if row1.isnull().values.any():
        for index2, row2 in df.iterrows():
            count = 0
            for col in df.columns:
                print (col)
                if row1[col] == row2[col] and index1 != index2:
                    count = count + 1
                else:
                    count = count
            if count > threshold*len(df.columns) and count < len(df.columns):
                row1.at[index1] = index2
                break

My input dataframe looks like this, so an example of what I am looking for is that row 2 should have the NaN replaced with the value of the column from row 1:
CODE   B2004    B2014   C2100   X3200   X1300
ID                  
20326   40.0    40.0    29.0    39.0    49.0
20338   40.0    NaN     29.0    39.0    49.0
20361   40.0    40.0    NaN     59.0    89.0
20381   40.0    40.0    NaN     59.0    NaN
20384   40.0    40.0    49.0    59.0    89.0
12385   40.0    40.0    29.0    29.0    55.0
12485   40.0    NaN     NaN     NaN     49.0
12492   35.0    35.0    NaN     NaN     49.0
12685   35.0    35.0    29.0    39.0    49.0
12687   40.0    NaN     29.0    29.0    55.0

The expected dataframe would be this:
CODE   B2004    B2014   C2100   X3200   X1300
ID                  
20326   40.0    40.0    29.0    39.0    49.0
20338   40.0    40.0    29.0    39.0    49.0
20361   40.0    40.0    49.0    59.0    89.0
20381   40.0    40.0    49.0    59.0    89.0
20384   40.0    40.0    49.0    59.0    89.0
12385   40.0    40.0    29.0    29.0    55.0
12485   40.0    NaN     NaN     NaN     49.0
12492   35.0    35.0    29.0    29.0    49.0
12685   35.0    35.0    29.0    39.0    49.0
12687   40.0    40.0    29.0    29.0    55.0

Any thoughts or ideas are appreciated!

A:

I figured out what was wrong. Since row is only a copy of the df, it wasn't actually assigning the value. By changing the 2nd to last row to
df.loc[index1] = row2

I was able to solve the issue

