HappyPy
HappyPy

Reputation: 10697

merging data frames with empty data frame

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

Upvotes: 0

Views: 189

Answers (2)

Prune
Prune

Reputation: 77837

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
>>> 

Upvotes: 2

jhso
jhso

Reputation: 3283

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')

Upvotes: 2

Related Questions