For the following DataFrame
my_cols = ["a", "b", "c"]
df2 = pd.DataFrame([["1a", "2a", "3a"], ["4aa", "5a", "6a"], ["7a", "8a", "9a"],
["1a", "2a", "3a"], ["4a", "5a", "6a"], ["7a", "8a", "9a"]],
columns=my_cols)
df2:
a b c
0 1a 2a 3a
1 4a 5a 6a
2 7a 8a 9a
3 1a 2a 3a
4 4a 5a 6a
5 7a 8a 9a
I want to evaluate if at any row a value contains the substring 4a. In that case I want to reassing in the whole row any a by b
my_str = "4a"
for x in range(df2.shape[0]):
if my_str in df2["a"][x]:
for y in range(len(my_cols)):
df2[my_cols[y]][x] = df2[my_cols[y]][x].replace("a","b")
df2:
a b c
0 1a 2a 3a
1 4ba 5b 6b
2 7a 8a 9a
3 1a 2a 3a
4 4b 5b 6b
5 7a 8a 9a
This method seems too inefficient, because of the multiple loops and the assignment done by replace(). Are there some built-in methods that could do the job? Any improvement will be appreciated.