使用来自其他数据框

时间:2017-06-12 16:36:01

标签: python pandas numpy

我有两个数据帧:

    术语:

  Drug                   Treatment      
  Ibuprofen 800mg        Hip Replacement Surgery
  Tylenol   AM           Mild Dislocation
  Advil                  Fractured Tibia
  Quinone                Fever
  Penicillin 250mg       Histerectomy
                         Surgical removal of bunion
                         Therapy
                         Bunion
data:
    Records  Physician Visit 1                        Physician Visit 2
    604     Take 2 tablets of advil & 3 caps of pen   Stop pen but cont. with advil
            250mg twice daily                         as needed for the fracture                                
    210     2 tabs of Tyl 3x daily for 5 days         nan
    515     nan                                       surgery scheduled for 01/01/2017
    019     Call my office if bunion pain persist     nan
            after 3 days

我希望使用TERMS TABLE中的术语列表从一张表格中提取更多的药物和治疗信息,并附上庞大的医生笔记。输出应该看起来像这样:

Records Visit1Drugs  Visit1Treatment  Visit2Drug   Visit2Treatment  Date
604     Advil,       nan              Advil        Fracture         nan
        Penicillin 
        250mg

210     Tylenol AM  nan               nan          nan              nan
515     nan         nan               nan          surgery          01/01/2017
019     nan         bunion            nan          nan              nan

我已经多次尝试将其变为现实,但最终我在每次尝试时都搞砸了我的代码。目前,这是唯一的功能代码,它只能提取整个记录/访问...这不是我希望实现的目标

drugs = terms['Drug'].tolist()
drugs = [x for x in drugs if str(x) != 'nan']
drugs = list(set(drugs))
visit = data['physician visit 1'].tolist()
visit = [x for x in visit if str(x) != 'nan']
visit = list(set(visit))

visitDrugList = []
for i in range(0, len(drugs)):
    search = ".*" + drugs[i]
    visitDrug = re.compile(search)
    visitDrugs = list(filter(visitDrug.match, visit))
    i+=1
    if visitDrugs:
        #print (search)
        visitDrugList.append(visitDrugs)
visitDrugList

我能够获得药物/访问列表,如果修改,可以获得治疗/访问列表。如何使用此提取的药物/治疗清单制作新表(如上所示)?我知道这不是pythonic,有更复杂的方法来完成这项工作 - 我正在寻找一个人来看我的问题并指出我正确的方向。谢谢大家!

0 个答案:

没有答案