在 Pandas DataFrame 中找到一个正则表达式并只修改它的一部分
Find a regex and modify only a part of it in a Pandas DataFrame
假设我有一些值,例如
test_val1 = 'E 18TH ST AND A AVE'
test_val2 = 'E 31ST ST AND A AVE'
我想找到第 18、31 等,并将其替换为 18/31 - 基本上删除后缀但保留整个字符串。
预期值
test_val1 = 'E 18 ST AND A AVE'
test_val2 = 'E 31 ST AND A AVE'
请注意,我不想删除 'street' 对应的“St”,因此无法盲目替换。
我的方法是使用下面的方法(目前 'th'),但它不起作用,因为该函数无法将内存中的 value/text 保留为 return。
import regex as re
test_val1.replace('\d{1,}TH', '\d{1,}', regex=True)
我有一列充满了这些值,所以我可以在 Pnadas 列上 run/apply 的解决方案真的很有帮助。
您提到它不起作用,因为该函数无法将内存中的 value/text 保存到 return 它。是否强制不将值存储到不同的变量?
t1 = 'E 18TH ST AND A AVE'
for t1 in column: #t1 is address in the dataframe column
t2 = t1.split()
t2[1] = re.sub(r'(TH|ST)', '',t2[1])
t1 = ' '.join(t2)
我想我可以帮助替换 REGEX。看起来您要使用的函数实际上是 sub
而不是 replace
。
这是函数签名:
re.sub(pattern, repl, string[, count, flags])
另外 here 是对类似问题的出色回答。
对于以下示例数据框
df = pd.DataFrame({"Test": ['E 18TH ST AND A AVE', 'E 31ST ST AND A AVE']})
Test
0 E 18TH ST AND A AVE
1 E 31ST ST AND A AVE
这个
df.Test = df.Test.str.replace(r'(\d+)(TH|ST)', lambda m: m.group(1), regex=True)
产生
Test
0 E 18 ST AND A AVE
1 E 31 ST AND A AVE
这就是您要找的吗?查看 docs 了解更多详情。
lambda
函数用作 repl
函数(“替换”),其 returns 替换字符串中的模式匹配。根据定义,它作为参数获取相应的 match object and has to return a string, usually derived from the match object, but it could be totally unrelated. The function here returns the content of the 1. capture group via the match object method group
:(\d+)
-part.
取样本值
test_val = 'E 32ND ST AND A AVE'
我用一个快速函数包装了它
import regex as re
def street_suffix_remover(in_val):
for x in re.findall(r"(\d{1,}TH|\d{1,}RD|\d{1,}ST|\d{1,}ND)", in_val):
in_val = in_val.replace(x, re.sub(r"TH|ST|RD|ND","", x))
return(in_val)
在样本上进行测试
street_suffix_remover(test_val)
输出
E 32 ST AND A AVE
运行 它在如下数据框上
test_df['address'] = test_df.apply(lambda row:street_suffix_remover(row['address']), axis=1)
假设我有一些值,例如
test_val1 = 'E 18TH ST AND A AVE'
test_val2 = 'E 31ST ST AND A AVE'
我想找到第 18、31 等,并将其替换为 18/31 - 基本上删除后缀但保留整个字符串。
预期值
test_val1 = 'E 18 ST AND A AVE'
test_val2 = 'E 31 ST AND A AVE'
请注意,我不想删除 'street' 对应的“St”,因此无法盲目替换。
我的方法是使用下面的方法(目前 'th'),但它不起作用,因为该函数无法将内存中的 value/text 保留为 return。
import regex as re
test_val1.replace('\d{1,}TH', '\d{1,}', regex=True)
我有一列充满了这些值,所以我可以在 Pnadas 列上 run/apply 的解决方案真的很有帮助。
您提到它不起作用,因为该函数无法将内存中的 value/text 保存到 return 它。是否强制不将值存储到不同的变量?
t1 = 'E 18TH ST AND A AVE'
for t1 in column: #t1 is address in the dataframe column
t2 = t1.split()
t2[1] = re.sub(r'(TH|ST)', '',t2[1])
t1 = ' '.join(t2)
我想我可以帮助替换 REGEX。看起来您要使用的函数实际上是 sub
而不是 replace
。
这是函数签名:
re.sub(pattern, repl, string[, count, flags])
另外 here 是对类似问题的出色回答。
对于以下示例数据框
df = pd.DataFrame({"Test": ['E 18TH ST AND A AVE', 'E 31ST ST AND A AVE']})
Test
0 E 18TH ST AND A AVE
1 E 31ST ST AND A AVE
这个
df.Test = df.Test.str.replace(r'(\d+)(TH|ST)', lambda m: m.group(1), regex=True)
产生
Test
0 E 18 ST AND A AVE
1 E 31 ST AND A AVE
这就是您要找的吗?查看 docs 了解更多详情。
lambda
函数用作 repl
函数(“替换”),其 returns 替换字符串中的模式匹配。根据定义,它作为参数获取相应的 match object and has to return a string, usually derived from the match object, but it could be totally unrelated. The function here returns the content of the 1. capture group via the match object method group
:(\d+)
-part.
取样本值
test_val = 'E 32ND ST AND A AVE'
我用一个快速函数包装了它
import regex as re
def street_suffix_remover(in_val):
for x in re.findall(r"(\d{1,}TH|\d{1,}RD|\d{1,}ST|\d{1,}ND)", in_val):
in_val = in_val.replace(x, re.sub(r"TH|ST|RD|ND","", x))
return(in_val)
在样本上进行测试
street_suffix_remover(test_val)
输出
E 32 ST AND A AVE
运行 它在如下数据框上
test_df['address'] = test_df.apply(lambda row:street_suffix_remover(row['address']), axis=1)