0

What would be the best way to insert a character conditionally in a PANDAS string field with the following criteria:

  1. Insert a "." after the first 3 numeric characters from the left (ignore letters)
  2. Do not add a "." if there are no additional characters trailing

    96501
    E0000
    V909
    965
    

    Becomes

    965.01
    E000.0
    V909
    965
    

1 Answer 1

1

You can capture the first three digits with ((?:\D*\d){3}), use (?=.+) to assert there is at least one more character following, and use back reference to add . to the captured pattern:

df[0].str.replace(r'^((?:\D*\d){3})(?=.+)', r'\1.')
#0    965.01
#1    E000.0
#2      V909
#3       965
#Name: 0, dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick answer! If you would not mind, what would also be a version that would count alpha and numeric from the left?
If both alpha and numeric, you can use ^((?:[^a-zA-Z0-9]*[a-zA-Z0-9]){3})(?=.+).

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.