【问题标题】:Match lists based on Name and DOB基于姓名和出生日期的匹配列表
【发布时间】:2016-11-30 13:52:48
【问题描述】:

这似乎应该很容易,但我似乎找不到我要找的东西...我有两个人员列表,名字、姓氏、出生日期,我只想知道哪个人们在两个列表中,哪些在一个列表中,而另一个不在另一个列表中。

我尝试过类似的东西

common = pd.merge(list1, list2, how='left', left_on=['Last', 'First', 'DOB'], right_on=['Patient Last Name', 'Patient First Name', 'Date of Birth']).dropna()

根据我在网上找到的其他内容,但它给了我这个错误:

KeyError: 'Date of Birth'

我已经验证这确实是第二个列表中的列标题,所以我不明白有什么问题。有人这样搭配吗?最简单/最快的方法是什么?列表之间的名称可能具有不同的格式,例如“Smith-Jones”与“SmithJones”与“Smith Jones”,但我通过从名称中删除所有空格和标点符号来解决这个问题......我认为这是第一个好的步骤?

【问题讨论】:

  • 查看集合的联合和差异。

标签: python


【解决方案1】:

试试这个,应该可以的

import sys
from StringIO import StringIO


import pandas as pd

TESTDATA=StringIO("""DOB;First;Last
    2016-07-26;John;smith
    2016-07-27;Mathew;George
    2016-07-28;Aryan;Singh
    2016-07-29;Ella;Gayau
    """)

list1 = pd.read_csv(TESTDATA, sep=";")

TESTDATA=StringIO("""Date of Birth;Patient First Name;Patient Last Name
    2016-07-26;John;smith
    2016-07-27;Mathew;XXX
    2016-07-28;Aryan;Singh
    2016-07-20;Ella;Gayau
    """)


list2 = pd.read_csv(TESTDATA, sep=";")

print list2
print list1

common = pd.merge(list1, list2, how='left', left_on=['Last', 'First', 'DOB'], right_on=['Patient Last Name', 'Patient First Name', 'Date of Birth']).dropna()
print common

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多