I am sorry if this question has been asked before or is trivial but I could not find an answer on here. Also, I am fairly new to python.
What I want to do is take one dataframe and 'add' other dataframes which have the same columns to it based on some key column. Those keys are not unique and appear multiple times. It might happen that the key does not appear in the dataframe to be added, then it should add "nan" but only in that row.
So this dataframe
df1 = pd.DataFrame({"X": [1,2,3,4], "KEY":['A','A','B','C']})
should be merged with this one
df2 = pd.DataFrame({"X": [6,7,8,9], "KEY":['A','A','B','B']})
and I want an outcome like this
df_merged = pd.DataFrame({"X_1": [1,2,3,4], "KEY":['A','A','B','C'], "X_2": [6,7,8,'nan']})
So if the key column value appears in the same row in the to be appended dataframe I want all the other columns in that row as well and if not just nans. I've tried several forms of joins and merges but I just cannot seem to figure it out. Help would be greatly appreciated!