I have the following two dataframes, which are snippets out of a large dataset:
df1: date key number 2000 1 50 2001 1 40 2000 2 600 2001 2 650 df2: key key2 1 A 2 B 3 C I want to add the key2 column to the df1 column matched on "key". The result should look the following:
date key number key2 2000 1 50 A 2001 1 40 A 2000 2 600 B 2001 2 650 B To do this, I am using the following command:
result = pd.merge(df1, df2, how="left", on="key") However, this also adds the key2 "C" to the dataset, which I do not want to be added. I only want the variable key2 be appended to the df1 based on the keys of df1. The information in df2 which does not match on key in df1 should be dropped. Therefore, my result dataframe should have one column more than df1 and the exact amount of rows.
Does anybody know why merge "left" does not work here, because if I run the code like this, my result dataframe has 1 column more - as desired-, but also more rows than df1, which I do not want.