Kilian Smith
Kilian Smith

Reputation: 35

Merging Two Dataframes stacks rows instead of merging into one

I'm attempting to merge two dataframes using two columns as keys: "Date" and "Instrument"

Here is my code:

merge_df = pd.merge(df1 , df2,  how='outer', left_on=['Date','Instrument'], right_on = ['Date','Instrument'])

df1: enter image description here

df2: enter image description here

You'll notice that the row in each dataframe has the same instrument and date value: AEA000201011 & 2008-01-31.

The merged dataframe is stacking the two rows instead of combining them:

merged_df: enter image description here

I have ensured that the dataframe key columns dtypes match:

df1:enter image description here

df2: enter image description here

Any advice would be much appreciated!

Upvotes: 0

Views: 46

Answers (1)

Taha Kınık
Taha Kınık

Reputation: 69

Man I wish I could use add comment section.

Even though you've probably already tried, have tried to use "left" or "right" instead of "outer" Or for once check them like

df1["Instrument"].iloc[0] == df2["Instrument"].iloc[0]

Maybe they got some invisible chars in them. If it's like that you can try using strip() functions. Nothing other than these comes to my mind.

Upvotes: 2

Related Questions