0
empty = pd.DataFrame(columns=["x"])
df1 = pd.DataFrame({"x":[1],"a":[3]})
df2 = pd.DataFrame({"x":[1],"b":[6]})

I'd like to merge the df1 with empty.

    x  a 
0   1  3

I tried

empty.merge(df1, on=["x"])

but this returns an empty data frame. After successfully merging with df1, I'd like to be able to merge empty again with df2 so that it results in

    x  a  b 
0   1  3  6

2 Answers 2

2

If you don't specify 'how' in a merge function it defaults to an 'inner' join which HAS to match keys on both sides of the merge. If you change it to a 'left' or 'right' join when you merge it will preserve the data:

empty = pd.DataFrame(columns=["x"])
df1 = pd.DataFrame({"x":[1],"a":[3]})
df2 = pd.DataFrame({"x":[1],"b":[6]})
df3 = empty.merge(df1, on=["x"],how='right').merge(df2,how='right')
Sign up to request clarification or add additional context in comments.

Comments

2

This is clear in the documentation: merge defaults to an inner join. Since there are no keys in your left table, you get no keys in the result. Fix this simply by specifying an outer join:

empty.merge(df1, how="inner", on=["x"])

Full demonstration:

>>> accum = empty.merge(df1, "outer")
>>> accum
   x  a
0  1  3
>>> accum = accum.merge(df2, "outer")
>>> accum
   x  a  b
0  1  3  6
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.