Conditional merge/replacement in R

use match(), assuming values in df1 are unique. df1 <- data.frame(x1=1:4,x2=letters[1:4],stringsAsFactors=FALSE) df2 <- data.frame(x1=2:3,x2=c(“zz”,”qq”),stringsAsFactors=FALSE) df1$x2[match(df2$x1,df1$x1)] <- df2$x2 > df1 x1 x2 1 1 a 2 2 zz 3 3 qq 4 4 d If the values aren’t unique, use : for(id in 1:nrow(df2)){ df1$x2[df1$x1 %in% df2$x1[id]] <- df2$x2[id] }

How to test if a string contains one of the substrings in a list, in pandas?

One option is just to use the regex | character to try to match each of the substrings in the words in your Series s (still using str.contains). You can construct the regex by joining the words in searchfor with |: >>> searchfor = [‘og’, ‘at’] >>> s[s.str.contains(‘|’.join(searchfor))] 0 cat 1 hat 2 dog 3 … Read more