I have a dataframe:
id value
4_french:k_15 10
87_john:k_82 82
11_mark:k_10/k_70 10
1_italian:k_11 9
I want to rename values in column id which have john:k_ giving them new id john or mark if its mark:k_ so desired result must be:
id value
4_french:k_15 10
john 82
mark 10
1_italian:k_11 9
How to do that?
CodePudding user response:
We can use str.replace here:
df["id"] = df["id"].str.replace(r'^.*(john|mark):k_.*$', r'\1')
Here is a regex demo showing that the replacement logic is working.
A generic version which assumes that you don't even know what the names might be is:
df["id"] = df["id"].str.replace(r'^.*([a-z] ):k_.*$', r'\1')
