'How do I check a specific value in a DataFrame is not unique, if not unique then I need to check the value of another cell in df then append True

I need to go through my df looking to see if the row starting with OBR and col3 is unique I have named this value Acc.

In this DataFrame I care about 2 variables Acc and MRN the Acc variable is always in the 3rd column and there is an OBR in the first column of the row containing the Acc variable. The MRN is similar it is in the 3rd column and there is always a PID in the first column of that row. The MRN can come in 2 forms A number followed by ^^^WANT (i.e. 28444302^^^WANT) or it can come as just the ^^^WANT with no number.

If Acc is unique and by unique I mean that in the DataFrame there is no other Acc with the same value. More specific is that there is no other value in column 3 with a row starting with OBR having the same value. If this is the case then I would append 'unique to the end of that row.

If there is an Acc that is the same as another Acc in different row then I need to check the MRN for that section (the sections are broke up by the /n rows) if all of the MRNs in each of the sections with the same Acc come back in the form a number followed by ^^^WANT (i.e. 60003212^^^WANT) then you can skip them.

if there is an Acc that is the same aalue as another Acc in a different row and one or more of the MRNs are just the value ^^^WANT then I want to append 'Not Unique' to the end of the rows with the same Acc value.

I have a DataFrame that looks like this:

   col1  col2         col3  col4   col5   col6  col7  col8
0   MSH     a            b     e      e      r     a     a
1   PID     b  MRN1^^^WANT     d      s  PickB     b     b
2   OBR     e         Acc1        PickD      v     e     e
3   OBX     g            h     e             s           g
4   ORC     i            j     p      p      p     i     i
5    /n  none         nine  none   none   none  none  none
6   MSH     a            b     e      e      r     a     a
7   PID     c      ^^^WANT     d      s  PickF     c     c
8   OBX     g            h     e      z      s     g     g
9   ORC     i            j            p      p     i     i
10  OBR     e         Acc2        PickG      v           e
11  OBX     g            h     e             s     g     g
12  OBX     i            j     p      p      p     i     i
13  OBX     g            h     e             s     g      
14  OBX     i            j     p      p      p     i     i
15   \n  none         none  none   none   none  none  none
16  MSH     a            b     e      e      r     a     a
17  PID     c  MRN3^^^WANT     d      s  PickH     c     c
18  OBX     g            h     e      z      s     g     g
19  ORC     i            j            p      p     i     i
20  OBR     e         Acc3        PickJ      v     e     e
21   \n  none         none  none   none   none  none  none
22  MSH     a            b     e      e      r     a     a
23  PID     c      ^^^WANT     d      s  PickK     c     c
24  OBR     e         Acc1        PickM      v     e     e
25  OBX     g            h     e             s     g      

Expected output would look like this:

   col1  col2         col3  col4   col5   col6  col7  col8  col9
0   MSH     a            b     e      e      r     a     a
1   PID     b  MRN1^^^WANT     d      s  PickB     b     b
2   OBR     e         Acc1        PickD      v     e     e  Not Unique
3   OBX     g            h     e             s           g
4   ORC     i            j     p      p      p     i     i
5    /n  none         nine  none   none   none  none  none
6   MSH     a            b     e      e      r     a     a
7   PID     c      ^^^WANT     d      s  PickF     c     c
8   OBX     g            h     e      z      s     g     g
9   ORC     i            j            p      p     i     i
10  OBR     e         Acc2        PickG      v           e  unique
11  OBX     g            h     e             s     g     g
12  OBX     i            j     p      p      p     i     i
13  OBX     g            h     e             s     g      
14  OBX     i            j     p      p      p     i     i
15   \n  none         none  none   none   none  none  none
16  MSH     a            b     e      e      r     a     a
17  PID     c  MRN3^^^WANT     d      s  PickH     c     c  
18  OBX     g            h     e      z      s     g     g
19  ORC     i            j            p      p     i     i
20  OBR     e         Acc3        PickJ      v     e     e  unique
21   \n  none         none  none   none   none  none  none
22  MSH     a            b     e      e      r     a     a
23  PID     c      ^^^WANT     d      s  PickK     c     c
24  OBR     e         Acc1        PickM      v     e     e  Not Unique
25  OBX     g            h     e             s     g      

Here is the df:

{'col1': ['MSH',
  'PID',
  'OBR',
  'OBX',
  'ORC',
  '/n',
  'MSH',
  'PID',
  'OBX',
  'ORC',
  'OBR',
  'OBX',
  'OBX',
  'OBX',
  'OBX',
  '\n',
  'MSH',
  'PID',
  'OBX',
  'ORC',
  'OBR',
  '\n',
  'MSH',
  'PID',
  'OBR',
  'OBX'],
 'col2': ['a',
  'b',
  'e',
  'g',
  'i',
  'none',
  'a',
  'c',
  'g',
  'i',
  'e',
  'g',
  'i',
  'g',
  'i',
  'none',
  'a',
  'c',
  'g',
  'i',
  'e',
  'none',
  'a',
  'c',
  'e',
  'g'],
 'col3': ['b',
  'MRN1^^^WANT',
  'Acc1',
  'h',
  'j',
  'nine',
  'b',
  '^^^WANT',
  'h',
  'j',
  'Acc2',
  'h',
  'j',
  'h',
  'j',
  'none',
  'b',
  'MRN3^^^WANT',
  'h',
  'j',
  'Acc3',
  'none',
  'b',
  '^^^WANT',
  'Acc1',
  'h'],
 'col4': ['e',
  'd',
  '',
  'e',
  'p',
  'none',
  'e',
  'd',
  'e',
  '',
  '',
  'e',
  'p',
  'e',
  'p',
  'none',
  'e',
  'd',
  'e',
  '',
  '',
  'none',
  'e',
  'd',
  '',
  'e'],
 'col5': ['e',
  's',
  'PickD',
  '',
  'p',
  'none',
  'e',
  's',
  'z',
  'p',
  'PickG',
  '',
  'p',
  '',
  'p',
  'none',
  'e',
  's',
  'z',
  'p',
  'PickJ',
  'none',
  'e',
  's',
  'PickM',
  ''],
 'col6': ['r',
  'PickB',
  'v',
  's',
  'p',
  'none',
  'r',
  'PickF',
  's',
  'p',
  'v',
  's',
  'p',
  's',
  'p',
  'none',
  'r',
  'PickH',
  's',
  'p',
  'v',
  'none',
  'r',
  'PickK',
  'v',
  's'],
 'col7': ['a',
  'b',
  'e',
  '',
  'i',
  'none',
  'a',
  'c',
  'g',
  'i',
  '',
  'g',
  'i',
  'g',
  'i',
  'none',
  'a',
  'c',
  'g',
  'i',
  'e',
  'none',
  'a',
  'c',
  'e',
  'g'],
 'col8': ['a',
  'b',
  'e',
  'g',
  'i',
  'none',
  'a',
  'c',
  'g',
  'i',
  'e',
  'g',
  'i',
  '',
  'i',
  'none',
  'a',
  'c',
  'g',
  'i',
  'e',
  'none',
  'a',
  'c',
  'e',
  '']}

Any Help would be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source