1

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?

french_fries
  • 1,149
  • 6
  • 22

1 Answers1

0

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).

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76