How do I concisely replace column values given multiple conditions?


political scientist

I'm trying to use to replace string values within a column; if string contains a keyword, I need the whole string to be replaced with another keyword (there are +- 25 combinations).

df["new_col"] =
df["col"].str.contains("cat1", na=False, case=False),
df["col"].str.contains("cat2", na=False, case=False),
df["col"].str.contains("cat3", na=False, case=False),
df["col"].str.contains("cat4", na=False, case=False),
# ...
df["col"].str.contains("cat25", na=False, case=False),
# ...

Is there a more concise way, or should I just repeat str.contains(...) within condlist 25 times?; is the proper way to do it, at all?

I assume dict could be used here, but don't see how exactly.

df["col"].map(d) where d is a dict with old and new values like {"cat1":"NEW_cat1"} wouldn't work (?) since I can't hardcode exact values that need to be replaced (and that's why I'm using str.contains).

Continue reading...