0

I have 3 dataframes that I'd like to combine. They look like this:

df1       |df2        |df3
col1 col2 |col1 col2  |col1 col3
1    5     2    9      1    some
                       2    data

I'd like the first two df-s to be merged into the third df based on col1, so the desired output is

df3
col1 col3 col2
1    some 5
2    data 9

How can I achieve this? I'm trying:

df3['col2'] = df1[df1.col1 == df3.col1].col2 if df1[df1.col1 == df3.col1].col2 is not None else df2[df2.col1 == df3.col1].col2 

For this I get ValueError: Series lengths must match to compare

It is guaranteed, that df3's col1 values are present either in df1 or df2. What's the way to do this? PLEASE NOTE, that a simple concat will not work, since there is other data in df3, not just col1.

1
  • 1
    IIUC you can do df3.merge(df1, on='col1', how='outer').merge(df2, on='col1', how='outer') Commented Mar 14, 2017 at 14:50

1 Answer 1

1

If df1 and df2 don't have duplicates in col1, you can try this:

pd.concat([df1, df2]).merge(df3)

enter image description here


Data:

df1 = pd.DataFrame({'col1': [1], 'col2': [5]})
df2 = pd.DataFrame({'col1': [2], 'col2': [9]})
df3 = pd.DataFrame({'col1': [1,2], 'col3': ['some', 'data']})
Sign up to request clarification or add additional context in comments.

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.