I have the following dataframe A:

        score   content  content_preprocessed   for_analysis
1         5       a             aaa                 True
2         5       b             NaN                 False
3         1       c             ccc                 True
4         1       d             ddd                 True

I need to replace content_preprocessed column with the following new column from dataframe B:

1              aaz  
3              ccf     
4              ddo   

which contains new values for content_preprocessed where for_analysis is set to True. The values for content_preprocessed should remain NaN if the corresponding for_analysis is False.

How do I do that?


1 Answer 1


Try this:

A.loc[A.for_analysis & (A.index.isin(B.index)) , content_preprocessed] = B.content_preprocessed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.