我有一个数据帧,如下所示:
col 1 col 2
0 59 538 Walton Avenue, Chester, FY6 7NP
1 62 42 Chesterton Road, Peterborough, FR7 2NY
2 179 3 Wallbridge Street, Essex, 4HG 3HT
3 180 6 Stevenage Avenue, Coventry, 7PY 9NP
列表类似于:
[Stevenage, Essex, Coventry, Chester]
按照这里的解决方案:How to check if Pandas rows contain any full string or substring of a list?如下所示:
city_list = list(cities["name"])
df["col3"] = np.where(df["col2"].str.contains('|'.join(city_list)), df["col2"], '')
我发现col2中的一些字符串与列表中的字符串匹配,但col3与col2相同。我希望col3是列表中的值,而不是与col3相同的值。这将是:
col 1 col 2 col3
0 59 538 Walton Avenue, Chester, FY6 7NP Chester
1 62 42 Chesterton Road, Peterborough, FR7 2NY
2 179 3 Wallbridge Street, Essex, 4HG 3HT Essex
3 180 6 Stevenage Avenue, Coventry, 7PY 9NP Coventry
我试过:
pat = "|".join(cities.name)
df.insert(0, "name", df["col2"].str.extract('(' + pat + ')', expand = False))
但这返回了一个错误,在预期为1时显示456个输入
此外:
df["col2"] = df["col2"].apply(lambda x: difflib.get_close_matches(x, cities["name"])[0])
df.merge(cities)
但这是错误列表索引超出范围后返回的
有什么办法可以这样做吗?df1大约有160000个条目,每个地址在col2中来自不同的国家,因此没有标准的书写方式,而城市列表大约有170000个条目
多谢各位
依靠这样的辅助功能:
你可以这样做:
查看
str.contains
函数,该函数测试模式是否与序列匹配:相关问题 更多 >
编程相关推荐