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