I have a dataframe:
id value
4_french:k_15 10
87_nov:k_82 82
11_nov:k_10 10
1_italian:k_11 9
I want to rename values in column id which have nov:k_
giving them new id k_10
or k_82
so desired result must be:
id value
4_french:k_15 10
k_82 82
k_10 10
1_italian:k_11 9
How to do that? I know about str.replace()
but how to keep number at the end?
CodePudding user response:
As you mentioned you can use str.replace
as follows:
df["id"] = df["id"].str.replace("^. _nov:", "", regex=True)
print(df)
Output
id value
0 4_french:k_15 10
1 k_82 82
2 k_10 10
3 1_italian:k_11 9
The pattern "^. _nov:"
will remove anything from the start of the string to "_nov:"
(included).