通过部分匹配大于n个字符的单词的两列来子集行

2024-05-16 15:05:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我愿意在python pandas中这样做,但在R中我有以下df:

    result<-structure(list(traffic_Count_Street = c("San Angelo", "W Commerce St", 
"W Commerce St", "S Gevers St", "Austin Hwy", "W Evergreen St"
), unit_Street = c("San Pedro Ave", "W Commerce", "W Commerce", 
"S New Braunfels", "Austin Highway", "W Cypress")), .Names = c("traffic_Count_Street", 
"unit_Street"), row.names = c(1L, 17L, 18L, 34L, 260L, 273L), class = "data.frame")

1             San Angelo   San Pedro Ave
17         W Commerce St      W Commerce
18         W Commerce St      W Commerce
34           S Gevers St S New Braunfels
260           Austin Hwy  Austin Highway
273       W Evergreen St       W Cypress

对于每一行,如果其中一个大于3个字符的单词与另一个匹配,我想部分匹配第1列到第2列。你知道吗

我将删除:

1             San Angelo   San Pedro Ave
34           S Gevers St   S New Braunfels
273       W Evergreen St   W Cypress

并保持:

17         W Commerce St      W Commerce
18         W Commerce St      W Commerce
260           Austin Hwy  Austin Highway

我尝试用以下方式使用stringR,但没有成功:

result$unit_Street[str_detect(result$traffic_Count_Street, "\\w{3}")]


Tags: streetcountunitresultcommercestsantraffic
1条回答
网友
1楼 · 发布于 2024-05-16 15:05:06

创建具有阈值调整的距离过滤器。然后你可以调整,直到你得到你想要的结果。在这种情况下,Levenshtein距离为5效果良好:

distanceFilter <- function(df, thresh=5) {
  ind <- apply(df, 1, function(x) adist(x[1], x[2]) < thresh )
  df[ind,]
}

distanceFilter(result, 5)
#     traffic_Count_Street    unit_Street
# 17         W Commerce St     W Commerce
# 18         W Commerce St     W Commerce
# 260           Austin Hwy Austin Highway

要了解更多信息,请参见the wiki pageR doc help page

相关问题 更多 >