比较两组数据框(字符串)

问题描述 投票:0回答:1

我有两个 pandas 数据框:test_df 和 Golden_df。每个帧中都有邮政地址。基本上,test_df 是用户输入的,可能有几个错误(缺少街道/pincode/格式不正确/等) - 我已使用 Google API 来纠正这些问题并格式化 test_df 的地址。 接下来,使用 Golden_df - 我必须查看 test_df 中存在哪些地址,哪些不存在。

我不是专家,因此我得到的第一个 isea 是使用 for 循环读取 test_df 中的每个地址并在 Golden_df 中搜索它 - 是的,这有效 - 但我知道必须有一种更有效的方法。 我的数据集很大 - 它可能最多包含 150000 行。我在下面分享了一些代表性的内容:

#test_df:

data = ["941 Thorpe St, Rock Springs, WY 82901",
    "2809 Harris Dr, Antioch, CA 94509",
    "7 Eucalyptus, Newport Coast, CA 92657",
    "725 Mountain View St, Altadena, CA 91001",
    "1966 Clinton Ave, Calexico, CA 92231",
    "431 6th St, West Sacramento, CA 95605",
    "5574 Old Goodrich Rd, Clarence, NY 14031",
    "Valencia Way, Valley Center, CA 92082"]
test_df = pd.DataFrame(data, columns=['parsed addresses'])
#golden_df:

data = ["941 Thorpe St, Rock Springs, WY 82901",
    "2809 Harris Dr, Antioch, CA 94509",
    "8838 La Jolla Scenic Dr N, La Jolla, CA 92037",
    "16404 Parthenia St North Hills, CA 91343",
    "1966 Clinton Ave, Calexico, CA 92231",
    "431 6th St, West Sacramento, CA 95605",
    "1010 Hillcroft Rd, Glendale, CA 91207",
    "Valencia Way, Valley Center, CA 92082"]
golden_df = pd.DataFrame(data, columns=['golden addresses'])

这就是我开始写的:

for i in range(0, len(test_df)):
    each_add = test_df.at[i,"parsed addresses"]
    golden_df['golden addresses'].str.contains(row_add) 

考虑到行数 - 最好的方法是什么?

我的目标是获得正确匹配的数量。

编辑添加最新更新:

我正在努力:

data_intersection = gold_add.loc[gold_add["golden address"].isin(test_add["parsed address"]),"golden address"]

我看到

len(data_intersection)
是 > test_add.shape[0]

如果函数计算测试数据帧和黄金数据帧中的通用数据,这怎么可能是真的?

python pandas string search geopy
1个回答
2
投票

使用

isin

>>> golden_df.loc[golden_df["golden addresses"].isin(test_df["parsed addresses"]),"golden addresses"]

0    941 Thorpe St, Rock Springs, WY 82901
1        2809 Harris Dr, Antioch, CA 94509
4     1966 Clinton Ave, Calexico, CA 92231
5    431 6th St, West Sacramento, CA 95605
7    Valencia Way, Valley Center, CA 92082
Name: golden addresses, dtype: object
© www.soinside.com 2019 - 2024. All rights reserved.