Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial Map with a dictionary in Pandas (python), Return attributes error

Tags:

python

pandas

The following is a sub-set of a dictionary:

equiv= {'Urin problem': 'C0/ Urine', 'disorientation': 'C0233407 / Disorientation', 'fatigue': 'C0015672 / Fatigue', 'headache': 'C334 / headache'} 

I am going to map the following data to this dictionary:

drug_id       WD
lexapro.1   minor urin problem
lexapro.1   Fatigue
lexapro.1   disorientation
lexapro.1   tiredness
lexapro.14  dizziness
lexapro.14  headaches

The output should be like this:

drug_id        WD                           Map              Exact_Match

lexapro.1   minor urin problem      C0/ Urine       
lexapro.1   Fatigue                'C0015672 / Fatigue                 1
lexapro.1   disorientation          C0233407 / Disorientation          1
lexapro.1   tiredness           
lexapro.14  dizziness           
lexapro.14  headaches                C334 / headache 

As you see if it can find the exact map, the column of Exact_Match will be filled with 1, like Fatigue and disorientation. And if the item is partial map, the Map column will have the corresponding value, but The exact match column does not get any value. This is my code:

df['Map'] = df["WD"].str.extract('('+'|'.join(list(equiv))+')').map(equiv).fillna(1), 

but it has the following error: AttributeError: 'DataFrame' object has no attribute 'map'. But when I use this code: df['Map'] = df["WD"].map(equiv).fillna(1), there is no error. However it does not help with partial match. I also do not know how to fill in the Exact_Match column with 1 when there is exact match.

like image 994
Mary Avatar asked Aug 25 '26 11:08

Mary


1 Answers

you can do it using the following vectorized approach:

mp = pd.DataFrame({'WD':[x.lower() for x in equiv.keys()],
                   'Map':[x for x in equiv.values()]})
df['Map'] = \
    df.WD.str.lower().replace(
        (r'.*\b' + mp.WD + r'[\b\n\r]*.*').tolist(),
        mp.Map.tolist(),
        regex=True
    )
df['Exact_Match'] = df.WD.str.lower().isin(mp.WD.str.lower()).astype(np.uint8)
df.loc[df.WD.eq(df.Map), 'Map'] = ''

Demo:

In [47]: mp = pd.DataFrame({'WD':[x.lower() for x in equiv.keys()],
    ...:                    'Map':[x for x in equiv.values()]})
    ...:

In [48]: mp
Out[48]:
                         Map              WD
0            C334 / headache        headache
1                  C0/ Urine    urin problem
2         C0015672 / Fatigue         fatigue
3  C0233407 / Disorientation  disorientation

In [49]: df['Map'] = \
    ...:     df.WD.str.lower().replace(
    ...:         (r'.*\b' + mp.WD + r'[\b\n\r]*.*').tolist(),
    ...:         mp.Map.tolist(),
    ...:         regex=True
    ...:     )
    ...:

In [50]: df['Exact_Match'] = df.WD.str.lower().isin(mp.WD.str.lower()).astype(np.uint8)

In [51]: df.loc[df.WD.eq(df.Map), 'Map'] = ''

In [52]: df
Out[52]:
      drug_id                  WD                        Map  Exact_Match
0   lexapro.1  minor urin problem                  C0/ Urine            0
1   lexapro.1             Fatigue         C0015672 / Fatigue            1
2   lexapro.1      disorientation  C0233407 / Disorientation            1
3   lexapro.1           tiredness                                       0
4  lexapro.14           dizziness                                       0
5  lexapro.14           headaches            C334 / headache            0
like image 68
MaxU - stop WAR against UA Avatar answered Aug 27 '26 00:08

MaxU - stop WAR against UA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!