gpt4 book ai didi

python - 从python中的字符串中删除前导文本字符

转载 作者:太空宇宙 更新时间:2023-11-03 14:36:40 24 4
gpt4 key购买 nike

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

我想提取第一个数字之后的所有数据,包括数字。请参阅数据框中的解决方案列。所以像“hospital2019 lane”这样的东西会变成“2019 lane”。

我试过按照下面的路线看东西,但我挣扎着用头撞墙。请让我知道我的方式错误。

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

最佳答案

使用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)

关于python - 从python中的字符串中删除前导文本字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57596458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com