5

reference: Pandas DataFrame: remove unwanted parts from strings in a column

In reference to an answer provided in the link above. I've researched some regular expressions and I plan to dive deeper but in the meantime I could use some help.

My dataframe is something like:

df:

  c_contofficeID
0           0109
1           0109
2           3434
3         123434  
4         1255N9
5           0109
6         123434
7           55N9
8           5599
9           0109

Psuedo Code

If the first two characters are a 12 remove them. Or alternatively, add a 12 to the characters that don't have a 12 in the first two characters.

Result would look like:

  c_contofficeID
0           0109
1           0109
2           3434
3           3434  
4           55N9
5           0109
6           3434
7           55N9
8           5599
9           0109

I'm using the answer from the link above as a starting point:

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'\D',value=r'')

I've tried the following:

Attempt 1)

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'[1][2]',value=r'')

Attempt 2)

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'$[1][2]',value=r'')

Attempt 3)

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'?[1]?[2]',value=r'')
2
  • ^12 is the regex for "starts with '12'" Commented Oct 26, 2016 at 22:53
  • 1
    What if you have "1234"? Should "12" be retained in that case or discarded? Commented Oct 26, 2016 at 23:05

1 Answer 1

2

new answers
per comment from @Addison

# '12(?=.{4}$)' makes sure we have a 12 followed by exactly 4 something elses
df.c_contofficeID.str.replace('^12(?=.{4}$)', '')

If ID's must have four characters, it's simpler to

df.c_contofficeID.str[-4:]

old answer
use str.replace

df.c_contofficeID.str.replace('^12', '').to_frame()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This is dangerous, as it won't work for 1234. Please use something like ^12(?=.{4}$)

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.