replace regex character if it has numbers in front of it

corianne1234

I would like to replace a letter if there is a number in front of it. So for example if I have 1K I would like to replace the K with 000, so that I have 1000. But If I have AKA then I would like to leave it the way it is. How would I do this? I tried "([0-9]+)k", "\\1000" but this didn't work. For examle this doesn't work:

df=pd.DataFrame({'xx':['100K', 'AKA', '1K', '10M']})
df.replace({"([0-9]+)K", "\\1000"}, regex=True)
Shubham Sharma

Try this:

df = df.replace(r"((?<=\d)K)", "000", regex=True)
df = df.replace(r"((?<=\d)M)", "000000", regex=True)

print(df)

Output:

         xx
0    100000
1       AKA
2      1000
3  10000000

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python regex: replace numbers with a special character

RegEx to find if string has first a character and then numbers

regex: Replace a character in a match. No matter what position it has

Regex - Replace repeated character

Regex replace escape character

XSLT - regex replace character

Regex replace special character

Regex to replace character in Javascript

Regex for numbers with spaces and + sign in front

How do i use regex to get a string that only has numbers in front and characters in back?

replace range of numbers with single numbers in a character string

Regex for allowing character, numbers and - in javascript

Regex check if string only has numbers after last instance of character and before last instance of another

Replace character in regex match only

Replace a specific character in a regex match

How to replace a character sequence with regex?

Regex to match and replace a character in a pattern

How to replace a specific character in regex?

Regex Capture Character and Replace with another

replace the nth character of a string (RegEx or not)

regex replace space with tab character

How to replace a character by regex in ruby

Powershell - Regex & Split / Replace Character

Regex: Replace "something" by a unicode character

Regex to replace character with character itself and hyphen

Replace each character of regex match by a another character

Regex substitution does not replace match character for character

Regex with any character in front and after given string

Regex Replace of strings of numbers into strings

TOP Ranking

HotTag

Archive