python 模糊匹配 合并_Python Pandas模糊合并/匹配重复

我目前有2个数据帧,1个用于捐赠者,1个用于筹款.理想情况下,我想要找到的是,如果有任何筹款人也捐赠,如果是的话,将一些信息复制到我的募捐人数据集(捐赠者姓名,电子邮件和他们的第一次捐赠).我的数据有问题

1)我需要通过姓名和电子邮件进行匹配,但用户可能会略有不同的名称(前Kat和Kathy).

2)捐赠者和筹款人的名称重复.

2a)有了捐赠者,我可以得到唯一的姓名/电子邮件组合,因为我只关心第一个捐赠日期

2b)虽然我需要保留两行,而不是像日期一样丢失数据.

我现在的示例代码:

import pandas as pd

import datetime

from fuzzywuzzy import fuzz

import difflib

donors = pd.DataFrame({"name": pd.Series(["John Doe","John Doe","Tom Smith","Jane Doe","Jane Doe","Kat test"]), "Email": pd.Series(['a@a.ca','a@a.ca','b@b.ca','c@c.ca','something@a.ca','d@d.ca']),"Date": (["27/03/2013 10:00:00 AM","1/03/2013 10:39:00 AM","2/03/2013 10:39:00 AM","3/03/2013 10:39:00 AM","4/03/2013 10:39:00 AM","27/03/2013 10:39:00 AM"])})

fundraisers = pd.DataFrame({"name": pd.Series(["John Doe","John Doe","Kathy test","Tes Ester", "Jane Doe"]),"Email": pd.Series(['a@a.ca','a@a.ca','d@d.ca','asdf@asdf.ca','something@a.ca']),"Date": pd.Series(["2/03/2013 10:39:00 AM","27/03/2013 11:39:00 AM","3/03/2013 10:39:00 AM","4/03/2013 10:40:00 AM","27/03/2013 10:39:00 AM"])})

donors["Date"] = pd.to_datetime(donors["Date"], dayfirst=True)

fundraisers["Date"] = pd.to_datetime(donors["Date"], dayfirst=True)

donors["code"] = donors.apply(lambda row: str(row['name'])+' '+str(row['Email']), axis=1)

idx = donors.groupby('code')["Date"].transform(min) == donors['Date']

donors = donors[idx].reset_index().drop('index',1)

因此,这给了我每个捐赠者的第一次捐赠(假设任何具有完全相同名称和电子邮件的人都是同一个人).

理想情况下,我希望我的筹款人数据集看起来像:

Date Email name Donor Name Donor Email Donor Date

2013-03-27 10:00:00 a@a.ca John Doe John Doe a@a.ca 2013-03-27 10:00:00

2013-01-03 10:39:00 a@a.ca John Doe John Doe a@a.ca 2013-03-27 10:00:00

2013-02-03 10:39:00 d@d.ca Kathy test Kat test d@d.ca 2013-03-27 10:39:00

2013-03-03 10:39:00 asdf@asdf.ca Tes Ester

2013-04-03 10:39:00 something@a.ca Jane Doe Jane Doe something@a.ca 2013-04-03 10:39:00

我尝试了这个帖子:is it possible to do fuzzy match merge with python pandas?但是不断让索引超出范围错误(猜测它不喜欢筹款活动中的重复名称):(那么任何想法如何匹配/合并这些数据集?

用for循环做它(它工作但速度很慢,我觉得必须有更好的方法)

fundraisers["donor name"] = ""

fundraisers["donor email"] = ""

fundraisers["donor date"] = ""

for donindex in range(len(donors.index)):

max = 75

for funindex in range(len(fundraisers.index)):

aname = donors["name"][donindex]

comp = fundraisers["name"][funindex]

ratio = fuzz.ratio(aname, comp)

if ratio > max:

if (donors["Email"][donindex] == fundraisers["Email"][funindex]):

ratio *= 2

max = ratio

fundraisers["donor name"][funindex] = aname

fundraisers["donor email"][funindex] = donors["Email"][donindex]

fundraisers["donor date"][funindex] = donors["Date"][donindex]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值