0

I use python and pandas.

I want to make like a [df_result]. df1's and df2's a,b is condition. if a,b is equal value, change the df1's 'd' column value by df2's 'd' column value.

how to make it? I don't know any solution..

df1

a b c d

1 2 5 1

1 5 5 1

2 3 4 1

df2

a b d

1 2 2

1 2 2

2 3 4

df_result

a b c d

1 2 5 2

1 5 5 1

2 3 4 4

1 Answer 1

1

I think you need numpy.where if same length and same index values in both DataFrames with comparing both columns with DataFrame.all:

df1['d'] = np.where((df1[['a', 'b']] == df2[['a', 'b']]).all(axis=1), df2['d'], df1['d'])
print (df1)
   a  b  c  d
0  1  2  5  2
1  1  5  5  1
2  2  3  4  4

print (df1[['a', 'b']] == df2[['a', 'b']])
      a      b
0  True   True
1  True  False
2  True   True

print ((df1[['a', 'b']] == df2[['a', 'b']]).all(axis=1))
0     True
1    False
2     True
dtype: bool

Another more general solution for matching by merge with left join, but is necessary unique rows in df2 by columns a and b by drop_duplicates, last combine_first and remove unnecessary column d_:

df = (df1.merge(df2.drop_duplicates(['a','b']), on=['a','b'], how='left', suffixes=('_',''))
         .assign(d= lambda x: x['d'].combine_first(x['d_']))
         .drop('d_', axis=1))

print (df)
   a  b  c    d
0  1  2  5  2.0
1  1  5  5  1.0
2  2  3  4  4.0

print (df2.drop_duplicates(['a','b']))
   a  b  d
0  1  2  2
2  2  3  4

print (df1.merge(df2.drop_duplicates(['a','b']), on=['a','b'], how='left', suffixes=('_','')))
   a  b  c  d_    d
0  1  2  5   1  2.0
1  1  5  5   1  NaN
2  2  3  4   1  4.0
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.