简体   繁体   中英

Word - List Dictionary

Here`s my dataframe

    drug        reaction     
0   OMALIZUMAB  Forced expiratory volume decreased  
1   OMALIZUMAB  Upper limb fracture 
2   OMALIZUMAB  Blood pressure abnormal 
3   OMALIZUMAB  Forced expiratory volume decreased  
4   OMALIZUMAB  Anxiety 
5   OMALIZUMAB  Asthma  
6   OMALIZUMAB  Fall    
7   NITROGLYCERIN   Product substitution issue  
8   OMALIZUMAB  Patella fracture    
9   OMALIZUMAB  Anxiety 

What I want is to dictionary the reaction of the drug. There are several reaction for a drug.

so output what I want is

{drug : [reaction], drug: [reaction], ...}

Example is only part of dataframe, and real data is bigger than ex.(600,000rows)

How can I get the result I want?

Maybe answers in this issue can work for you too;

Ignoring NaNs with str.contains

You can try the command below referencing to Harry_pb's answer.

df[df['dr.prod_ai'].str.contains("foo") == True]

Use defaultdict, with a list option to group the items based on the keys.
In this case the keys are from the drug column

from collections import defaultdict
d=defaultdict(list)

Use a list comprehension to make it faster

[d[i].append(j) for i,j in zip(df.drug, df.reaction)]

print(d)

defaultdict(list,
        {'OMALIZUMAB': ['Forced expiratory volume decreased',
          'Upper limb fracture',
          'Blood pressure abnormal',
          'Forced expiratory volume decreased',
          'Anxiety',
          'Asthma',
          'Fall',
          'Patella fracture',
          'Anxiety'],
         'NITROGLYCERIN': ['Product substitution issue']})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM