0

I have following pandas dataframe (umls)

             CUI      SDUI  SAB  TTY                    STR
325040  C0011405   D003788  MSH   MH   Dental Pulp Diseases
325054  C0011405  10012328  MDR  LLT   Dental pulp disorder
325055  C0011405  10012328  MDR   PT   Dental pulp disorder
325057  C0011405  10044050  MDR   HT  Dental pulp disorders
325061  C0011405   D003788  MSH  DEV          PULP DIS DENT
325062  C0011405   D003788  MSH  DEV          DENT PULP DIS
325063  C0011405   D003788  MSH  DEV          DIS DENT PULP

I would like to filter rows based on certain conditions like: When SAB = MSH, select TTY= MH and when SAB = MDR, select TTY= LLT and PT.

I am expecting below output:

             CUI      SDUI  SAB  TTY                    STR
325040  C0011405   D003788  MSH   MH   Dental Pulp Diseases
325054  C0011405  10012328  MDR  LLT   Dental pulp disorder
325055  C0011405  10012328  MDR   PT   Dental pulp disorder

I am using following lines of code:

umls[(umls['SAB'].isin(['MSH', 'MDR']))] & (umls['TTY'].isin(['MH', 'LLT', 'PT']))]

Any help is highly appreciated

1
  • the filtering may be simpler via a MultiIndex Commented Oct 13, 2022 at 12:14

1 Answer 1

2

Remove ()[] for chain both masks:

df = umls[umls['SAB'].isin(['MSH', 'MDR']) & umls['TTY'].isin(['MH', 'LLT', 'PT'])]
print (df)
             CUI      SDUI  SAB  TTY                   STR
325040  C0011405   D003788  MSH   MH  Dental Pulp Diseases
325054  C0011405  10012328  MDR  LLT  Dental pulp disorder
325055  C0011405  10012328  MDR   PT  Dental pulp disorder
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.