Vinny B
Vinny B

Reputation: 55

Pandas Merge Multiple Columns

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

Upvotes: 0

Views: 438

Answers (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'],
)

Upvotes: 1

Related Questions