1

I have a master data frame and auxiliary data frame. Both have the same timestamp index and columns with master having few more columns. I want to copy a certain column's data from aux to master.

My code:

maindf = pd.DataFrame({'A':[0.0,NaN],'B':[10,20],'C':[100,200],},index=pd.date_range(start='2020-05-04 08:00:00', freq='1h', periods=2))
auxdf= pd.DataFrame({'A':[1,2],'B':[30,40],},index=pd.date_range(start='2020-05-04 08:00:00', freq='1h', periods=2))

maindf = 
                       A   B    C
2020-05-04 08:00:00  0.0  10  100
2020-05-04 09:00:00  NaN  20  200

auxdf = 
                     A   B
2020-05-04 08:00:00  1  30
2020-05-04 09:00:00  2  40

Expected answer: I want o take column A data in auxdf and copy to maindf by matching the index.

maindf = 
                     A   B    C
2020-05-04 08:00:00  1  10  100
2020-05-04 09:00:00  2  20  200

My solution:

maindf['A'] = auxdf['A']

My solution is not correct because I am copying values directly without checking for matching index. how do I achieve the solution?

1 Answer 1

2

You can use .update(), as follows:

maindf['A'].update(auxdf['A'])

.update() uses non-NA values from passed Series to make updates. Aligns on index.

Note also that the original dtype of maindf['A'] is retained: remains as float type even when auxdf['A'] is of int type.

Result:

print(maindf)

                       A   B    C
2020-05-04 08:00:00  1.0  10  100
2020-05-04 09:00:00  2.0  20  200
Sign up to request clarification or add additional context in comments.

3 Comments

so, .update() replaces all values in maindf whether they are nan, or floats with the values from auxdf. Right?
@Mainland It updates only matching index / indexes (row and column indexes for dataframe). One point to note is that it takes only non-NA value from the auxiliary df / series.
@Mainland You can refer to the link I provided above for Series.update() for more examples on Series operations. Also see the doc for DataFrame.update() for examples for dataframe.

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.