0

I am struggling to merge two pandas dataframes to replicate a vlookup function using two columns as lookup value.

The first dataframe df has 6 columns including three columns: perf, ticker and date. The perf column is empty and this is the one I would like to see populated. The second dataframe u includes the same three columns, including values in the perf column but only for a specific date.

I have tried this: df=pd.merge(df,u,how='left',on=['ticker_and_exch_code', 'date'])

But the result I get is a dataframe with new perf columns instead of populating the one existing perf column. Would really appreciate insights into what I am missing, thanks!

Vincent

2
  • Did you assign the new column to your existing dataframe? df["perf"] = pd.merge(df,u,how='left',on=['ticker_and_exch_code', 'date'])["perf"]? Commented Nov 10, 2020 at 14:04
  • Tried that but I get an error, Key Error 'perf', indexer = self.columns.get_loc(key) Commented Nov 10, 2020 at 14:23

1 Answer 1

1

If the 'perf' column is empty in the first DataFrame, may I suggest removing it before merging the two DataFrames?

df=pd.merge(
    df.drop(columns='perf'),
    u,
    how='left',
    on=['ticker_and_exch_code', 'date'],
)
Sign up to request clarification or add additional context in comments.

3 Comments

Tried but this does not solve the issue. The merge creates new columns perf_x and perf_y every time the merge operation is performed.
Can you double check before running this that you don't have any other columns stored in df? the code above should work if at running time df has only one 'perf' column. do print(df.columns) just before merge to check. if you working in jupyter notebooks it's possible that your df already stored some 'perf_x' or 'perf_y' columns from previous runs.
Are you perhaps conducting the same merging operation multiple times in a row? therefore re-creating the 'perf' column before the next merge? If so, we will need a more advanced solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.