I have two DataFrames like here:
df1 = sent token token2 0 a b 0 a c 0 b d 1 g h 1 h k 1 h i 1 g i 1 g k df2 = sent token token2 rel 0 a b A 1 g h B 1 k g C Now I want to merge those two DataFrames into one which should look like this:
df_new = sent token token2 rel 0 a b A 0 a c NaN 0 b d NaN 1 g h B 1 h k NaN 1 h i NaN 1 g i NaN 1 g k C However merging the DataFrames like this
df_new = df1.merge(df2, on=["sent","token","token2"], how="left")
I get the output I want except the ["rel"] token is wrong
df_new = sent token token2 rel 0 a b A 0 a c NaN 0 b d NaN 1 g h B 1 h k NaN 1 h i NaN 1 g i NaN 1 g k NaN This is due to the order of the token in df1. Since the value in ["rel"] depends on ["token1"] -> ["token2"] it can't apply its value when the order is reversed. Is there any way to do this in the merging process without creating a new version of df1 ?
df1to include all possible combinations of token and token2.