pandas remove duplicates from a cell

Viewed 111

I need to remove the duplicate numbers in a cell and duplicate percentage sign in cell

I have two inputs:

Input1-

  1. 51 %
  2. 4% %
  3. (0.9 %)
  4. 53 %
  5. 44 %
  6. 29 %

Name: Paid Loss Ratio , dtype: object

Req'd output1 :

  1. 51
  2. 4
  3. -0.9
  4. 53
  5. 44
  6. 29

Name: Paid Loss Ratio , dtype: object

The following script works for this case:

f1['Paid Loss Ratio '].str.split('%').apply(set).str.join('').str.rstrip()

Trying to remove the duplicated "% sign in one cell"

input2 -

  1.   88 % 
    
  2.  102 % 
    
  3. 84 84 %
  4.   ( 0.8 %
    
  5.   93 % 
    

Name: Constant Loss Ratio , dtype: object

Req'd Output2 :

  1. 88 %
  2. 102 %
  3. 84 %
  4. -0.8%
  5. 93 %

Trying the following but it doesn't work properly:

re.sub(r'([0-9])(.*)\1+', r'\1', str(ab))

Trying to remove the duplicated "84" number in one cell

Also is there a way to merge these two scripts/pattern?

1 Answers

Your requirement on the first input doesn't match the output since you only seem to want to remove a double percentage sign? If I'm correct I think you could use:

(\S+)(?=\s\1)\s

See an online demo


A sample Python script could be:

df = df.replace(r'(\S+)(?=\s\1)\s','', regex=True)

Prints:

0    51 %
1     4 %
2     9 %
3    53 %
4    44 %
5    29 %
6    88 %
7   102 %
8    84 %
9     Nan
10   93 %
Related