1

My 1st DataFrame is so:

RowCol Value_Pre Key1 234 Key3 235 Key2 235 Key4 237

My Second Dataframe is:

RowCol Value_Post Key3 235 Key1 334 Key4 237 Key2 435

How to create a third dataframe like a the one below by combining the two dataframe

RowCol Value_Pre Value_Post Key1	234	334 Key3	235	235 Key2	235	435 Key4	237	237

How to develop this code in Python?

1 Answer 1

1

Use map:

df1['Value_Post'] = df1['RowCol'].map(df2.set_index('RowCol')['Value_Post']) print (df1) RowCol Value_Pre Value_Post 0 Key1 234 334 1 Key3 235 235 2 Key2 235 435 3 Key4 237 237 

Or merge with left join, especially if necessary append multiple new columns:

df = df1.merge(df2, on='RowCol', how='left') 
Sign up to request clarification or add additional context in comments.

2 Comments

How do you create another column with values depending if Value_Pre is not equal to Value_Post?
@Adhil - Do you think df1['new'] = np.where(df1['Value_Pre'].ne(df1['Value_Post']), 10, 20) ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.