简体   繁体   中英

Removing leading text characters from string in python

import pandas as pd
import re
df = pd.DataFrame({'fix_this_field':['dogstreet 1234, st, texas 57500', 'animal hospital of dallas, 233 medical ln '], 'needed solution':['1234, st texas 57500', '233 medical ln']})
df #look what i want

I want to extract all of the data after the first number, including the number. See solution column in dataframe. So something like 'hospital2019 lane' would become '2019 lane'.

I have tried looking something along the lines of what is below but I am struggling and banging head against the wall. Please let me know error of my ways.

x = 'hospital2019 lane'
r = re.compile("^([a-zA-Z]+)([0-9]+)")
m = r.match(x)
m.groups()
# it stops at 2019.   I want 2019 lane.....('hospital', '2019')

Easy to achieve by using split

df.fix_this_field.str.split('(\d)',1).str[1:].apply(''.join)
Out[475]: 
0    1234, st, texas 57500
1          233 medical ln 
Name: fix_this_field, dtype: object
df['col']=df.fix_this_field.str.split('(\d)',1).str[1:].apply(''.join)

If you must use regex, below is an attempt:

  • Regex: (?:[a-zA-Z ])([0-9]+.*)
reg = re.compile('(?:[a-zA-Z ,])([0-9]+.*)')

def clean(col):
    return re.findall(reg, col)[0] if re.findall(reg, col) else None

df.fix_this_field.apply(clean)

Out[1]:
0    1234, st, texas 57500
1          233 medical ln 
Name: fix_this_field, dtype: object

我找到了df.fix_this_field.apply(lambda x: x[re.search("\\d",x).start():])df.fix_this_field.apply(lambda x: ''.join(re.split('(\\d)',x,1)[1:]))几倍于df.fix_this_field.str.split('(\\d)',1).str[1:].apply(''.join)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM