Basically I have a dataframe that could look like this:
ID NAME PAINT
0 some_name:target blue
1 some_other_name pink
2 other_name: other_target yellow
3 other_name black
And only want to replace values that follow a certain regex by applying a function to them.
def f(x):
name, target = x.split(":")
return "[" + target + "]" + " " + name
ID NAME PAINT
0 [target] some_name blue
1 some_other_name pink
2 [other_target] other_name yellow
3 other_name black
I imagine it would look something like this but whatever works
df.replace(to_replace=strings_found_by_regex, value=f(strings_found_by_regex))
This could probably be done by iterating over rows and seing if those cells match the regex and then appplying f(x) but that looks rather ugly and I wondered whether there is a better way.