0

I have a dataframe df1 with 3 columns (A,B,C), NaN represents missing value here

A     B      C  
1     2    NaN
2     1    2.3
2     3    2.5

I have a dataframe df2 with 3 columns (A,B,D)

A     B     D
1     2     2
2     1     2
2     3     4

The expected output would be

A     B      C
1     2      2
2     1      2.3
2     3      2.5

I want to have values in column C in df1 intact if not missing, replaced by corresponding value in D with other two columns value equal, i.e, df1.A==df2.A and df1.B==df2.B

any good solution?

1
  • 3
    Also, add sample df2 and expected output like df1 Commented Jul 4, 2017 at 17:06

2 Answers 2

1

One way would be to use the columns A and B as the index. If you use fillna then, pandas will align the indices and give you the correct result:

df1.set_index(['A', 'B'])['C'].fillna(df2.set_index(['A', 'B'])['D']).reset_index()
Out: 
   A  B    C
0  1  2  2.0
1  2  1  2.3
2  2  3  2.5
Sign up to request clarification or add additional context in comments.

Comments

1

IIUC:

In [100]: df['C'] = np.where((np.isnan(df.C))&(df.A==df1.A)&(df.B==df1.B),df1.D,df.C)

In [101]: df
Out[101]: 
     A    B    C
0  1.0  2.0  2.0
1  2.0  1.0  2.3
2  2.3  1.2  2.5

np.where is faster when compared:

In [102]: %timeit df['C'] = np.where((np.isnan(df.C))&(df.A==df1.A)&(df.B==df1.B),df1.D,df.C)
1000 loops, best of 3: 1.3 ms per loop


In [103]: %timeit df.set_index(['A', 'B'])['C'].fillna(df1.set_index(['A', 'B'])['D']).reset_index()
100 loops, best of 3: 5.92 ms per loop

3 Comments

This will only work if df1 and df2 have the same length, and common observations are in the same order. df1['C'].fillna(df2['D']) would work in that case too.
yes, what if df1 and df2 does not have the same length?
@ayhan I have not thought about that i will try and let you know. Thank you for pointing out the mistake.

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.