Use indexing and mapmap to replace letters:
df.iloc[:, 2:] = df.apply(lambda x: x[2:].map(x[:2]), axis=1)
print(df)
# Output:
A B i j y z
0 1 2 2 1 1 1
1 2 3 3 2 3 3
Setup:
df = pd.DataFrame({'A': [1, 2], 'B': [2, 3], 'i': ['B', 'B'],
'j': ['A', 'A'], 'y': ['A', 'B'], 'z': ['A', 'B']})
print(df)
# Output:
A B i j y z
0 1 2 B A A A
1 2 3 B A B B
Details:
For each row, apply the following function over index axis so x contains the whole row at each iteration:
Map the value from the third column (x[2:] <- i, j, y, z) to the index from the two first columns (x[:2] <- A, B) like a dictionary (a Series can act as dictionary, check the map method)
For the first iteration:
A 1 # <- index A
B 2 # <- index B
i B # <- value B
j A # <- value A
y A # <- value A
z A # <- value A
Name: 0, dtype: object