For loop to replace value in one dataframe with X if it appears in another dataframe

As Kraigolas commented, you can easily do this without looping.

Check if elements are in another array with np.in1d and then map truth values to "X" and "Y":

import pandas as pd
import numpy as np


df1 = pd.DataFrame()
df1["col1"] = ["A", "B", "C", "D", "E"]
df2 = pd.DataFrame()
df2["col2"] = ["A", "C", "D"]

df1["col3"] = list(map(lambda x: "X" if x else "Y", np.in1d(df1.col1, df2.col2)))
print(df1)

Output:

  col1 col3
0    A    X
1    B    Y
2    C    X
3    D    X
4    E    Y